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

  • SEARCH
  • Home
  • 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 7013987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:27:43+00:00 2026-05-27T22:27:43+00:00

I am using an approach similar to T. Stone’s answer on this question .

  • 0

I am using an approach similar to T. Stone’s answer on this question. However, I have added an abstract base class, so my models.py looks like this:

class CustomQuerySetManager(models.Manager):
    """A re-usable Manager to access a custom QuerySet"""
    def __getattr__(self, attr, *args):
        try:
            return getattr(self.__class__, attr, *args)
        except AttributeError:
            return getattr(self.get_query_set(), attr, *args)

    def get_query_set(self):
        return self.model.QuerySet(self.model)

class MyModel(models.Model): 
    class Meta:
        abstract = True

    class QuerySet(QuerySet):
        def user(self, pub, *args, **kwargs):
            return self.filter(publisher=pub, *args, **kwargs)

    ...some more methods here

class Book(MyModel):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author, related_name='book_author')
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    objects=models.Manager()
    obj=CustomQuerySetManager() #for testing purposes only, this will override objects later

This allows me to get all of the books for a given publisher like such:

p = Publisher.object.get(pk=1)
Book.obj.user(p).all()

I would like to extend this so I can define a custom query in the Book model then pass a Q object to the QuerySet class, so the query “publisher=pub” can be different for different models. I still want to be able to call this like Book.obj.user(p).all(). Somewhere in the Book model I need:

pubQ=Q(publisher=pub)

Where can I put this and how do I pass it to QuerySet defined in the Abstract Base Class, while keeping the code as DRY as possible?

  • 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-27T22:27:44+00:00Added an answer on May 27, 2026 at 10:27 pm

    That answer is clever, but it breaks the Python principle of “explicit is better than implicit”. My first reaction to your code was to tell you that you can’t declare a custom queryset inside your model, but I decided to check the mentioned SO answer to see where you got that idea from. Again, it’s clever — not discounting that, but well-written code is self-documenting and should be able to be picked up by any random Django developer and ran with. That’s where peer code-reviews come in handy — had you had one, you’d have instantly got a WTF with that.

    The Django core team does it the following way:

    class MyQuerySet(models.query.QuerySet):
        def some_method(self, an_arg, another_arg, a_kwarg='some_value'):
            # do something
            return a_queryset
    
    class MyManager(models.Manager):
        def get_query_set(self):
            return MyQuerySet(self.model)
    
        def some_method(self, *args, **kwargs):
            return self.get_query_set().some_method(*args, **kwargs)
    

    It’s DRY in the sense that you don’t repeat the actual method definition in the manager. But, it’s also explicit — you know exactly what’s going on. It’s not as DRY as the method you’re referencing, but “explicit is better than implicit”. Besides if it’s done that way in the actual Django codebase, you can be reasonably assured that it’s good practice to do so in your own code. And, it has the side-effect of making it much easier to extend and override in subclasses.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using an approach for this thread in order to make gallery change one
I'm using this approach to store data in a global array hosting an http
I was reading today about OOCSS which says by using that approach have 2
So, I'm using this approach to be able to drag an image: Dragging an
I'm using a naive approach to this problem, I'm putting the words in a
What is the best approach in using a timer. Use a System.Timer.Timer class or
I am having some trouble understanding this brute force approach. I am using Perl
This question is being based off of a similar question titled, ASP.NET MVC Relative
Sorry if my last question is very similar, but I have another query of
I am using CodeFirst approach and struck with an issue where I need to

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.