Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7572453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:55:45+00:00 2026-05-30T15:55:45+00:00

I have these two models class Genre( TimeStampAwareModel ): genre = models.CharField ( max_length

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:55:46+00:00Added an answer on May 30, 2026 at 3:55 pm

    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:

    from django.db.models import Q
    
    Track.objects.filter(Q(genre__genre='Rock') | Q(genre__parent__genre='Rock')).distinct()
    

    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:

    Track.objects.filter(Q(genre__genre__in=['Rock', 'Pop']) | Q(genre__parent__genre__in=['Rock', 'Pop'])).distinct()
    

    UPDATE

    Ah, then it’s a bit more complicated but still doable.

    has_rock_genres = Q(genre__genre='Rock') | Q(genre__parent__genre='Rock')
    has_pop_genres = Q(genre__genre='Pop') | Q(genre__parent__genre='Pop')
    
    Track.objects.filter(has_rock_genres & has_pop_genres).distinct()
    

    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:

    query = None
    for genre in genres:
        if query is None:
            query = Q(genre__genre=genre) | Q(genre__parent__genre=genre)
        else:
            query = query & (Q(genre__genre=genre) | Q(genre__parent__genre=genre))
    
        Track.objects.filter(query).distinct()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two models: class CommonVehicle(models.Model): year = models.ForeignKey(Year) series = models.ForeignKey(Series) engine
I have these two models : class Module(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(unique=True,
In Django application I have these models: class DLL(models.Model): prev = models.ForeignKey('self', related_name =
Given these two models: class Profile(models.Model): user = models.ForeignKey(User, unique=True, verbose_name=_('user')) about = models.TextField(_('about'),
I have two models: class Video(models.Model): slug = models.SlugField(blank=True, default='', db_index=True, unique=True) class VideoTranslation(models.Model):
I have these models class User(models.Model): user_name = models.CharField() ph_number = models.CharField() class ExamPaper(models.Model):
I have a simple category model: class Category(models.Model): name = models.CharField(max_length=200) slug = models.SlugField()
I have these two models: class Application_Model_List extends Zend_Db_Table_Abstract { protected $_name = 'list';
I have these two models class Invoice < ActiveRecord::Base has_many :items accepts_nested_attributes_for :items ...
Say I have these two models: public class City extends Model { @ManyToOne private

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.