Categories
Python Answers

How to add a multiple choice field with Python Django?

Spread the love

Sometimes, we want to add a multiple choice field with Python Django.

In this article, we’ll look at how to add a multiple choice field with Python Django.

How to add a multiple choice field with Python Django?

To add a multiple choice field with Python Django, we can add a ManyToManyField.

For instance, we write

class Choices(models.Model):
  description = models.CharField(max_length=300)

class Profile(models.Model):
  user = models.ForeignKey(User, blank=True, unique=True, verbose_name='user')
  choices = models.ManyToManyField(Choices)

to create the choices field in the Profile model which is a ManyToManyField.

We use Choices as the argument to allow Choices values as the values for choices.

Conclusion

To add a multiple choice field with Python Django, we can add a ManyToManyField.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *