guinny
guinny

Reputation: 1532

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?

thanks for help!

Upvotes: 0

Views: 50

Answers (2)

J0HN
J0HN

Reputation: 26921

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

Upvotes: 1

msc
msc

Reputation: 3800

FlowerSpecies.objects.filter(months__month_idx=6)

Read up on the queries documentation, these things are well documented.

Upvotes: 1

Related Questions