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 8266577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T05:12:40+00:00 2026-06-08T05:12:40+00:00

I have a model with a custom manager with the purpose of filtering active

  • 0

I have a model with a custom manager with the purpose of filtering “active” objects, i.e. objects which have a start_date lower than the current time and an end_date greater than the current time.

This is the relevant part of my models.py:

from django.utils.timezone import now

class ActiveObjectManager(models.Manager):
    def get_query_set(self):
        return super(ActiveObjectManager, self).get_query_set().\
            filter(start_date__lt=now(), end_date__gt=now())

class Object(models.Model):
    start_date = models.DateTimeField(_('Service start date'), \
        auto_now_add=False, null=False, blank=False)
    end_date = models.DateTimeField(_('Service end date'), auto_now_add=False, \
        null=False, blank=False)
    ...
    objects = models.Manager()
    objects_active = ActiveObjectManager()

This manager works great across the application and in a Django shell. However, if I create an object in the admin interface, and set the start_date to the “now” selector, the API provided by tastypie isn’t showing this newly created object (though it does show older objects). The admin list correctly shows the new object as active.

This is the relevant part of my api.py:

from app.models import Object

class ActiveObjectResource(ModelResource):
    modified = fields.BooleanField(readonly=True)

    class Meta:
        resource_name = 'activeobjects'
        queryset = Object.objects_active.all()

My strong suspicion is that, as the class ActiveObjectResource is being interpreted once, the couple of now() calls are only being executed once, i.e., the API subsystem is always calling filter() with the same values for the start_date__lt and end_date__gt parameters (the value returned by now() immediately after I run manage.py runserver).

This problem persists even when I do the filtering right in the resource class like this:

class ActiveObjectResource(ModelResource):
    ...
    class Meta:
        queryset = Object.objects.\
            filter(start_date__lt=now(), end_date__gt=now())

Also, the problem persists if I pass callables like this:

class ActiveObjectResource(ModelResource):
    ...
    class Meta:
        queryset = Object.objects.filter(start_date__lt=now, end_date__gt=now)

Is there a way I can rewrite ActiveObjectManager or ActiveObjectResource to overcome this?

Update:
OK, it seems I need to override get_object_list to achieve per-request alterations to the queryset, like:

class ActiveObjectResource(ModelResource):
    class Meta:
        queryset = Object.objects.all()

    def get_object_list(self, request):
        return super(MyResource, self).get_object_list(request).\
            filter(start_date__lt=now, end_date__gt=now)

But I hate to duplicate this logic when I already have a custom manager at the model level to do this work for me.

So my question is: how can I use my custom model manager from within my ModelResource?

  • 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-06-08T05:12:41+00:00Added an answer on June 8, 2026 at 5:12 am

    Well, about queryset in ModelResource.Meta. Here’s the excerpt from the tastypie documentation:

    If you place any callables in this, they’ll only be evaluated once (when the Meta class is instantiated). This especially affects things that are date/time related. Please see the :ref:cookbook for a way around this.

    Here it goes:

    A common pattern is needing to limit a queryset by something that changes per-request, for instance the date/time. You can accomplish this by lightly modifying get_object_list

    So, yeah, seems like the only way to achieve what you are trying to do is to declare get_object_list.

    New Update: since get_object_list is just a return self._meta.queryset._clone(), try something like that:

    class ActiveObjectResource(ModelResource):
        class Meta:
            queryset = Object.objects_active.all()
    
        def get_object_list(self, request):
            return Object.objects_active.all()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom model binder which pulls an implementation of an interface from
I have two custom methods for a model manager in Django. One of them
I want to overwrite the custom objects model manager to only return objects a
I've created a custom Manager for a Django model which returns a QuerySet holding
I have a custom manager. I want to use it for related objects. I
I am newbie at Django. I have model with a custom method. In view
In my application I have a custom model binder that I set to the
I have a model class that uses a custom validator as shown in following
I have a Model that contains a List of a custom type. I want
I have a model Whitelabel and a User has_many :whitelables I have a custom

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.