Hi folks,

I have some models with a custom Manager that prefetches certain fields:

class PrefetchManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset()\
            .select_related(*self.model.select_fields)\
            .prefetch_related(*self.model.prefetch_fields)


class MyModel(models.Model):
    foo = models.ForeignKey(Foo)
    bars = models.ManyToManyField(Bar, related_name='mymodels')

    select_fields = ('foo',)
    prefetch_fields = ('bars')
    prefetched = PrefetchManager()


This allows the nice and succinct "MyModel.prefetched.filter(...)" to get model 
instances with prefetched fields. However, I would also like this to work with 
RelatedManager, i.e.:

    m = MyModel.objects.get(pk=1)
    m.bars.prefetched.filter(...)

to get all Bar connected to m with the prefetched fields I have defined on Bar. 
Is there a way to do this? Can I create a custom RelatedManager for 'bars'? I 
can rewrite it as:

   Bar.prefetched.filter(mymodels=m, ...)

but it feels more awkward.

Thanks,
Erik

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9E6B2519-C3BC-4820-B8A2-043E8EC450C2%40cederstrand.dk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to