On 22 juil. 2013, at 17:31, Loic Bistuer <loic.bist...@sixmedia.com> wrote:
> The goal is to make it painfully easy to get a `Manager` from a custom > `QuerySet` and for most cases, having the factory on `Manager` instead of > `QuerySet` results in a more verbose syntax and requires an unnecessary > import. Objects with strong knowledge of one another are an OO design anti-pattern, see <http://en.wikipedia.org/wiki/Circular_dependency>, linked from <http://en.wikipedia.org/wiki/Anti-pattern>. The QuerySet class is already quite a beast; let's keep its responsibilities clearly delineated. A sound design will save us pain in the future and is well worth a few extra keystrokes. For example CBVs are a similar case: Django went for MyView.as_view(**kwargs), which wasn't the shortest syntax, but allowed greater flexibility. > In any case, a factory is better than the `Manager(MyQuerySet)` syntax (may > it be through __new__, self.__class__, __getattr__ or __getattribute__) > because it lets us create the classes ahead of time and it works well with > inheritance. Yes, that sounds right. I see two options for defining this factory: - as a class method of the Manager class: this is the most obvious way to write a factory; - as a standalone function: this shouldn't be necessary here, as far as I can tell. Either way the factory should receive the QuerySet in argument as well as any other parameters required to create the custom Manager appropriately. If you really want the short and sweet API, I can listen for an argument to implement it as follows: class QuerySet: def as_manager(self, *args, **kwargs): # imported here because this method creates a circular dependency # between Manager and QuerySet from django.db.models import Manager return Manager.from_queryset(self, *args, **kwargs) (untested, I just typed that in my mailer) This alleviates my concerns about the circular dependency — it's fairly limited and controlled — but the natural location of a Manager factory is still on the Manager class… Make that a -0… -- Aymeric. -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To post to this group, send email to django-developers@googlegroups.com. Visit this group at http://groups.google.com/group/django-developers. For more options, visit https://groups.google.com/groups/opt_out.