_ in Django is a convention that is used for localizing texts. It is an alias for ugettext_lazy.
A ManyToMany field is used when a model needs to reference multiple instances of another model. Use cases include: A user needs to assign multiple categories to a blog post. A user wants to add multiple blog posts to a publication.
What is Django? Django is a high-level Python web framework that enables rapid development of secure and maintainable websites. Built by experienced developers, Django takes care of much of the hassle of web development, so you can focus on writing your app without needing to reinvent the wheel.
You created a foreign key on Choice
which relates each one to a Question
.
So, each Choice
explicitly has a question
field, which you declared in the model.
Django's ORM follows the relationship backwards from Question
too, automatically generating a field on each instance called foo_set
where Foo
is the model with a ForeignKey
field to that model.
choice_set
is a RelatedManager
which can create querysets of Choice
objects which relate to the Question
instance, e.g. q.choice_set.all()
If you don't like the foo_set
naming which Django chooses automatically, or if you have more than one foreign key to the same model and need to distinguish them, you can choose your own overriding name using the related_name
argument to ForeignKey
.
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