1

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?

1 Answer 1

1

The CharField should be a ChoiceField.

Create a form:

condition = models.ChoiceField(max_length=3, choices=CONDITION_CHOICES)
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, django will automatically generate a select field.
I created a form based on the model (edited above). Do I need to specify conditions as a choice field specifically? How would I go about doing that? Thanks for the help -- I'm still new at this!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.