Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a text box a choice field?

I want to display the choice field in forms.

This is my model but I am not able to see the select box, it displays as textarea.

SOURCE_CHOICES = Choices(
    ('var1', '1'),
    ('var2', '2')
)

source = models.TextField(choices=SOURCE_CHOICES, null=True, blank=True)

Do I need to put something in my form as well?

In my previous project I did the same thing. I defined everything in my model and saw the select box but not here.

like image 514
user19140477031 Avatar asked Dec 19 '12 08:12

user19140477031


1 Answers

You should use CharField instead TextField:

source = models.CharField(choices=SOURCE_CHOICES, max_length=3, ... )

TextField is rendered always as TextArea.

https://docs.djangoproject.com/en/dev/ref/models/fields/#choices

like image 79
dani herrera Avatar answered Sep 18 '22 01:09

dani herrera