Saturday, September 29, 2012

select_related and use_for_related_field

I have a custom manager class that has use_for_related_field set to true, and I set it to be the default manager in my model.  But when I make a query using select_related on the related field, I seem to get a plain queryset, not my custom one.  Here's a quick sketch of what I'm doing:

class MyQuerySet(models.query.QuerySet):
    @property
    def foo(self):
        return self.filter(foo=True)

class MyManager(models.Manager):
    use_for_related_field = True

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

class Widget(models.Model):
    thingies = models.ManyToManyField(Thingy, related_name='widgets')
    objects = MyManager()

When I run this query:

Thingy.objects.select_related('widgets').foo

Django tells me that

AttributeError: 'QuerySet' object has no attribute 'foo'

Is this something I should expect to work?  I seem to have reached the thin edge of both documentation and discussions.

John

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

No comments:

Post a Comment