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 :)
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.
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.
Set the exclude attribute of the ModelForm 's inner Meta class to a list of fields to be excluded from the form.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With