I have these two models
class Genre( TimeStampAwareModel ):
genre = models.CharField ( max_length = 255, blank = False )
parent = models.ForeignKey ( 'self', null=True, blank=True, related_name = "childs" )
..
class Track( TimeStampAwareModel ):
....
genre = models.ManyToManyField( Genre )
I have as input list of genres [Pop,Rock,..], since Pop and Rock have child genres too. Now i want to filter all tracks fulfilling following condition
(G1parent OR G1child1 OR G1child2 OR …..) AND (G2parent OR G2child1 OR G2child2 OR …..)
def get_genre_tracks(list_genre):
....
...
return tracks
here G1parent is Pop and G2parent is Rock, How can i get this? looking for an elegant solution.
Thanks!
If I understand you properly, you want all tracks that either have a genre of “Rock” or a genre that has “Rock” as a parent. If so:
EDIT
Actually, after re-reading the question it seems you want what I said, but for a list of genres together, instead of just one at a time. For that, you just need to tweak the above code like:
UPDATE
Ah, then it’s a bit more complicated but still doable.
You could do that all in one line, but the code becomes a bit of a mess at that point.
UPDATE
Really giving me a mental workout today, aren’t you? 😉
You’ll need to do something like: