Django tutorial choice_set

From the Django tutorial:

I defined my models as follows:

from django.db import models import datetime from django.utils import timezone # Create your models here. class Poll(models.Model): question = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __unicode__(self): # Python 3: def __str__(self): return self.question def was_published_recently(self): return self.pub_date >= timezone.now() - datetime.timedelta(days=1) class Choice(models.Model): poll = models.ForeignKey(Poll) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0) def __unicode__(self): # Python 3: def __str__(self): return self.choice_text 

Where is the choice_set parameter set and how does it work?

 >>> p = Poll.objects.get(pk=1) # Display any choices from the related object set -- none so far. >>> p.choice_set.all() 
+4
source share
2 answers

I don't know how deep the explanation you need, but Django defines it for you when you do poll = models.ForeignKey(Poll) .

You can read about it here.

+4
source

choice_set is not defined anywhere.

Django creates APIs for the β€œother” side of the relationship β€” a link from a related model to a model that defines the relationship. For example, a Poll p object has access to a list of all related Choice objects through the choice_set attribute: p.choice_set.all ().

So choice_set. Where selection is your lowercase selection model, and _set is the Django Manager tool.

You can read the details right here .

0
source

Source: https://habr.com/ru/post/1490665/


All Articles