S
S
sortfact3332020-11-22 09:00:35
Django
sortfact333, 2020-11-22 09:00:35

How to get value from ModelForm?

I have a model

class Car(models.Model):
  class ModelChoise(models.IntegerChoices):
    Model_1 = 0
    Model_2 = 1
    Model_3 = 2
    Model_4 = 3

  name = models.TextField('Name')
  model_id = models.IntegerField('model id', choices=ModelChoise.choices, default=1)

  def __str__(self):
    return self.name

And I created a shape for her
class CarForm(ModelForm):
  class Meta:
    model = ExtraUser
    fields = ['model_id']

I need to derive key and value from it
{% for  mykey,myvalue in form.model_id %}
    {{ mykey }} : {{ myvalue }}
{% endfor %}
<!--
    Model_1 : 0
    Model_2 : 1
    Model_3 : 2
    Model_4 : 3
-->

How can I do it? I can only get the key.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question