from django.db import models
# Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField(verbose_name='date published')
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
一(Question)对多(Choice)关系
1.前向查询
# 通过Choice类中的question字段属性
Choice.objects.filter(question__pub_date__year=current_year)
2.反向查询
# 通过classname_set
q=Question.objects.get(pk=1)
q.choice_set.filter(choice_text__startswith='Just hacking')