Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django, choices option field

i have a question about the choices option field.

i have this field:

SECUENCIA = (
             ('1','1'),
             ('2','2'),
             ('3','3'),
)
secuencia = models.IntegerField(max_length=1,choices=SECUENCIA)

All is fine in my forms for add or update but in my show view (template display) the field just appear like "(None)" and dont show the value (1 or 2 or 3).

Thanks :)

like image 329
Asinox Avatar asked Aug 14 '09 04:08

Asinox


People also ask

How do you define choice fields in Django?

Django Field Choices. According to documentation Field Choices are a sequence consisting itself of iterables of exactly two items (e.g. [(A, B), (A, B) …]) to use as choices for some field. For example, consider a field semester which can have options as { 1, 2, 3, 4, 5, 6 } only.

How do I make a field optional in Django?

In order to make a field optional, we have to say so explicitly. If we want to make the pub_time field optional, we add blank=True to the model, which tells Django's field validation that pub_time can be empty.

How do you exclude a specific field from a ModelForm?

Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.

What is slug field in Django?

What is SlugField in Django? It is a way of generating a valid URL, generally using data already obtained. For instance, using the title of an article to generate a URL. Let's assume our blog have a post with the title 'The Django book by Geeksforgeeks' with primary key id= 2.


1 Answers

The first element of your choice tuple has to be the value that will be stored. In your case, it needs to be an integer:

SECUENCIA = (
             (1, '1'),
             (2, '2'),
             (3, '3'),
)

See the documentation for more information:

  • http://docs.djangoproject.com/en/dev/ref/models/fields/#choices
like image 55
ars Avatar answered Oct 06 '22 01:10

ars