简体   繁体   English

Django过滤相关字段

[英]Django filtering for related fields

I have the following structure: 我有以下结构:

class FlowerSpecies(models.Model):
  pass

class Months(models.Model):
  flower_species = models.ForeignKey(FlowerSpecies)
  month_idx = models.IntegerField()

In words, I have a bunch of flower species each of which can grow in certain months only. 用语言来说,我有一堆花种,每种都只能在某些月份生长。

How can I now use filter to query for all the species that grow in June for example? 我现在如何使用过滤器查询6月份种植的所有物种?

thanks for help! 感谢帮助!

Should be able to do that with 应该可以做到这一点

FlowerSpecies.objects.filter(months__month_idx=6) #single month
FlowerSpecies.objects.filter(months__month_idx__in=(1,2,3)) #multiple months

See Django Making Queries documentation page for details 有关详细信息,请参阅Django Making Queries文档页面

FlowerSpecies.objects.filter(months__month_idx=6)

阅读查询文档 ,这些内容都有详细记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM