0

I have an integer field in db, which is option field. I'm saving the options in terms of id in to the db, but I also have to display the value in that field pertaining to certain operations. Can anyone here please tell me how to get a text value from an int field and display it? Below is my code:

Html:

<th col width="55%"><label><strong>Risk Estimation</strong></label>

               <select ng-model="arform.risk_estimate"
                       ng-options="item for item in risk_estimate">
                  <option style="display:none" value=" risk_estimate.text"></option>
               </select>
            </th>

Controller:

{{ngapp}}.controller(
        "SiVerifyAddReviewController",
        function($scope, $http, $modalInstance, r_header){

$scope.risk_estimate = ['High','Medium', 'Low'];};

Will be able to provide if further part of code is required for understanding. Thanks.

SURYA VISWANATHAN
  • 187
  • 1
  • 3
  • 12

2 Answers2

1

You're probably looking for this: https://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.get_FOO_display

{{ object.get_field_display }}
Andrey Shipilov
  • 1,986
  • 12
  • 14
  • It works. Thanks! Any Idea how to do for a checkbox field? get_field_display is for only for choices or can be used for checkbox too? – SURYA VISWANATHAN Oct 14 '16 at 06:17
  • @SURYAVISWANATHAN no it doesn't. It works only for choice made fields. But. You can create a cached_property on your model say `get_some_field_display` and inside that property return hardcoded string or do some calculations. Then in templates just do the usual {{ object.get_some_field_display }} – Andrey Shipilov Oct 14 '16 at 09:00
  • By cached_property, do you mean using patch_cache_control? If possible, Could you give me an example where I can use cached_property in model? I'm new here. Thanks. – SURYA VISWANATHAN Oct 17 '16 at 03:16
  • No, this. https://docs.djangoproject.com/en/1.10/ref/utils/#django.utils.functional.cached_property Just store the name of the field in a property of a model and you will have it templates too. – Andrey Shipilov Oct 19 '16 at 04:00
  • Thanks! It worked. I'm expecting an answer for one more Question I posted recently. If possible, Could you take a look at it and give your inputs? – SURYA VISWANATHAN Oct 19 '16 at 04:44
  • Below is the link for the Q http://stackoverflow.com/questions/40121484/pass-value-from-view-to-controller-and-display-in-template – SURYA VISWANATHAN Oct 19 '16 at 04:45
0

As per my understanding you need to get selected option text when ever it will change or submitted right?, then you need to represent all options like this in a way so you will get whole object of option which is selected, then you can extract the value of even text from it. Follow this:

<select  ng-model="selectOption" ng-change="changedValue(selectOption)" 
 data-ng-options="option as allOptions.name for option in allOptions">
<option value="">Select Account</option>

// Controller will be like this :

function ctrl($scope){
    $scope.itemList=[];
    $scope.allOptions=[{id:1,name:"a"},{id:2,name:"b"},{id:3,name:"c"}]

    $scope.changedValue=function(item){
    $scope.itemList.push(item.name);
    }    

}
Jigar7521
  • 1,549
  • 14
  • 27