Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django custom manager get_queryset() not working

I can't make my custom manager work...

class PublicArtigoManager(models.Manager):
    def get_queryset(self):
        return super(PublicArtigoManager, self).get_queryset().filter(data_publicacao__lte=timezone.now()).filter(permissao__lte=3)

class Artigo(models.Model):
    ...
    objects = models.Manager()
    publics = PublicArtigoManager()

when I test in the shell, It doesnt work

>>> from artigos.models import Artigo
>>> from django.utils import timezone
>>> print Artigo.objects.count()
9960
>>> print Artigo.publics.count()
9960
>>> print Artigo.objects.filter(data_publicacao__lte=timezone.now()).filter(permissao__lte=3).count()
9959

Artigo.publics.count() should return 9959, right? Any ideas what might be going wrong?

like image 662
Alexei Martchenko Avatar asked May 09 '13 18:05

Alexei Martchenko


1 Answers

I'm sure the problem is the get_query_set method. This is the doc for version 1.5 managers and it says:

You can override a Manager‘s base QuerySet by overriding the Manager.get_query_set() method. get_query_set() should return a QuerySet with the properties you require.

Try to do it with get_query_set instead of get_queryset which is how it is explained in the dev doc:

You can override a Manager‘s base QuerySet by overriding the Manager.get_queryset() method. get_queryset() should return a QuerySet with the properties you require.

If you want to be 100% positive about how the method is named in your version just go to your Manager class definition in django/db/models/manager.py and search how is named the method in the class.

Hope it helps!

like image 67
Paulo Bu Avatar answered Oct 16 '22 11:10

Paulo Bu