Alexandr Domoryonok Alexandr Domoryonok - 5 months ago 17
AngularJS Question

how to use django choice in angularJS

i have choice field in user model:

GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)

gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')


after serialize i get:
enter image description here

and in my template i want use radio buttons:

<div class="radio-item">
<div class="label-text">Пол</div>
<div class="item">
<input type="radio" name="sex" id="male">
<label for="male"><span></span> Male</label>
</div>
<div class="item">
<input type="radio" name="sex" id="female">
<label for="female"><span></span> Female</label>
</div>
</div>

Answer

It seems like you are getting data from API and binding to form. So in your case you could use ng-model over radio inputs.

But before that 1st thing you need to bind whole object to $scope.user = data;. Additionally I assumed that radio values would be M & F.

HTML

<div class="radio-item">
  <div class="label-text">Пол</div>
  <div class="item">
    <input type="radio" name="sex" id="male" ng-model="user.gender" value="M">
    <label for="male"><span></span> Male</label>
  </div>
  <div class="item">
     <input type="radio" name="sex" id="female" ng-model="user.gender" value="F">
     <label for="female"><span></span> Female</label>
   </div>
</div>
Comments