If you find yourself doing that a lot in your project, yes a custom manager is the way to go. Keep in mind that you can more then one manager attached to your model, but
the order matters. For the admin purposes, you can attach the default manager to your model, and call it by overriding the
get_queryset() ModelAdmin method from
here# models.py
class Article(models.Model):
....
objects = ExcludeExpiredManager()
with_expired_objects = models.Manager()
# admin.py
class ArticleAdmin(admin.ModelAdmin):
def get_queryset(self, request):
qs = self.model.with_expired_objects.get_queryset()
ordering = self.get_ordering(request)
if ordering:
qs = qs.order_by(*ordering)
return qs
On a personal note, I prefer defining custom queryset and use the
as_manager() method to get the manager, but in your case you might need the
from_queryset(), but I've never used it myself.