-
-
Save EricTatua/8ddf68a5895504891b84b86b53a9e811 to your computer and use it in GitHub Desktop.
Revisions
-
jhgaylor revised this gist
Jun 27, 2013 . 2 changed files with 19 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,15 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ class A(models.Model): things = models.ManyToManyField("B", through=ThroughModel) class B(models.Model): text = models.TextField() class ThroughModel(models.Model): a = models.ForeignKey(A) b = models.ForeignKey(B) extra = models.BooleanField() #this will return a list of ThroughModel objects ThroughModel.objects.filter(b=instance_of_b, extra=True) #this will return a list of A objects based on an extra field on the through table A.objects.filter(things__ThroughModel__extra=True) #keep in mind that limiting by one of the foreign keys on the through model is easier A.objects.filter(things=instance_of_b) -
jhgaylor created this gist
Jun 27, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ class A(models.Model): things = models.ManyToMany(through=Through) class B(models.Model): ... class Through(models.Model): a = models.ForeignKey(A) b = models.ForeignKey(B) extra = models.Boolean() #this would do it Through.objects.filter(b=instance_of_b, extra=True) #is there a way to go this way? A.things.filter(extra=True)