goelv
goelv

Reputation: 2884

How to create html dropdown/select menu for a model field that is defined with choices (Django)

I have the following model in Django:

class Product(models.Model):
  CONDITION_CHOICES = (
   ('N', 'New'),
   ('UN', 'Used - Like New'),
   ('UV', 'Used - Very Good'),
   ('UG', 'Used - Good'),
   ('UA', 'Used - Acceptable'),
 )
 condition = models.CharField(max_length=3, choices=CONDITION_CHOICES)

I intend to create a form out of this model as such:

class MattressForm(ModelForm):
  class Meta:
    model = Product

The end result (in the template / html) will be a dropdown / select menu where users can choose a single option.

I can't figure out how to write the html for this specific option. I don't want to use comprehensive tags like {{ form.as_p }}, etc... as I intend to customize each field in the form on my own.

Does any know how to create the dropdown / select menu for this type of model field?

Upvotes: 1

Views: 996

Answers (1)

user1301404
user1301404

Reputation:

The CharField should be a ChoiceField.

Create a form:

condition = models.ChoiceField(max_length=3, choices=CONDITION_CHOICES)

Upvotes: 1

Related Questions