QuerySet.as_manager doesn't offer a way to use_for_related_fields

336 views
Skip to first unread message

Martin Chase

unread,
Dec 30, 2014, 8:47:41 PM12/30/14
to django-d...@googlegroups.com
Hey all,

I make extensive use of managers, and I was excited to start using as_manager for my queryset/manager mashups, but it seems use_for_related_fields isn't a part of as_manager's considerations. I can think of two sensible implementations: pass an argument to as_manager(), or honor a class attr of the same name on the queryset.

Is this a reasonable improvement? Is one of those possible implementations (or a completely different one) better?

Regards,
Martin Chase

Markus Holtermann

unread,
Dec 31, 2014, 7:37:09 AM12/31/14
to django-d...@googlegroups.com
Hey Martin,

To solve the problem I can think of two reasonable solutions:

class MyManager(models.Manager.from_queryset(MyQuerySet)):
    use_for_related_fields = True

or

class MyModel(models.Model):
    objects = MyQuerySet.as_manager()
    objects.use_for_related_fields = True

After all, as_manager() calls from_queryset() internally.

I don't think a class attribute on the QS class makes much sense since as_manager() would be the only place where it is used. An argument to as_manager() would make more sense to me, though I'm not convinced this is really that much of an improvement.

From the two suggested solutions above, I'd tend to use the first one.

/Markus

Martin Chase

unread,
Dec 31, 2014, 1:54:21 PM12/31/14
to django-d...@googlegroups.com
Hey Marcus,

Thanks for the ideas. I haven't tested it, but the second solution is specifically mentioned as not working in the docs:

https://docs.djangoproject.com/en/1.7/topics/db/managers/#set-use-for-related-fields-when-you-define-the-class

Your first solution is what I have in place now (specifically, I have a little function that makes the manager using the solution you describe), and it does work.

This isn't a pressing need by any measure, it would just clean up the code a little and I thought it would make sense that as_manager should be able to make a manager with any basic manager behavior.

Regards,
Martin
Reply all
Reply to author
Forward
0 new messages