Thanks for the reply.

I previously had a look at how QuerySet.none (in
Django.db.models.query) was implemented, and that didn 't give me any
clues - hence the post.  So I worked it out by looking at
http://seanmonstar.com/post/708862164/extending-django-models-managers-and-querysets
which explains how QuerySet and manager methods are called through the
QuerySetManager.

 __getattr__ will be called prior to throwing an AttributeError to try
an find a manager method that isn't already implemented by the manager
**or any of its base classes**.   That is, I think it's pulling none
from the base class implementation.

So I guess a solution could be to create a class where overrides are
implemented, and let your QuerySetManager and custom QuerySet
implementations inherit from it - to save having to implement your
methods in the manager and your custom QuerySet.

This might not be the most elegant solution.  I'll look into the
Manager implementation as you suggested.

Thanks.


On Jul 27, 5:50 pm, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> On 27 juil, 06:27, gs794 <gregs...@gmail.com> wrote:
>
>
>
> > Hi all,
>
> > I'm having trouble overriding QuerySet.none() (code below)...  Works
> > if I rename the method to almost anything but "none".
>
> > class QuerySetManager(models.Manager):
> >   def get_query_set(self):
> >     return self.model.QuerySet(self.model)
> >   def __getattr__(self, name):
> >     return getattr(self.get_query_set(), name)
>
> > class NewNoneQuerySet(models.query.QuerySet):
> >   def none(self):
> >     print 'NewNone\n\n\n\n'
> >     return self.filter(pk__in=[])
>
> > class TestNone(models.Model):
> >   name = models.CharField(max_length=20)
> >   objects = QuerySetManager()
> >   class QuerySet(NewNoneQuerySet):
> >     pass
>
> > TestNone.objects.none()
>
> > No output of 'NewNone\n\n\n\n'... Using the inherited method in other
> > tests...  Is there something special about the none method?
>
> Django is open-source, so you can read the source and find out by
> yourself why your code doesn't work as expected (I just did, and took
> me about 2 minutes).
>
> FWIW, you want to have a look at how Manager.none is implemented (in /
> django/db/manager.py)
>
> HTH.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to