一個字段篩選:Django的在考慮從Django的投票教程以下車型的外鍵對象
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
was_published_recently.admin_order_field = 'pub_date'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently'
class Choice(models.Model):
question = models.ForeignKey(Question)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.choice_text
我希望能夠排除沒有問題,並選擇類似建議的教程。我一直在玩弄過濾器,但我無法弄清楚,我想:
def get_queryset(self):
return Question.objects.filter(pub_date__lte=timezone.now(), choice__count__gt=0)
,但我發現
Relation fields do not support nested lookups
我如何通過沒有任何問題過濾選擇?
這似乎更好,因爲它允許我通過任意數量的選項(如2或更多)進行過濾,而其他答案只會告訴我是否有一些或0個選項 – Jake 2014-11-03 06:45:09