It appears that not everyone knows that in python you can create classes dynamically without metaclasses. I'll show an example of how to do it.
So we've learned how to use custom QuerySet to chain requests:
Now we need to make it work for related objects:
This is done using use_for_related_fields, but it needs a little trick. Thing is that in django related managers are initialized without arguments, and our QuerySetManager takes argument - queryset class.
objects = QuerySetManager(MyQuerySet)
We have to update QuerySetManager in such a way, that will allow setting queryset as class field:
class QuerySetManager(models.Manager): use_for_related_fields = True queryset_class = QuerySet def get_query_set(self): return self.queryset_class(self.model, using=self._db) def __getattr__(self, key): return getattr(self.get_query_set(), key)
In order to use this manager, we have to create a sublcass and set queryset_class:
class MyManager(QuerySetManager): queryset_class = MyQuerySet
It's not fun to create such class every time. So we'll write a function that does it for us:
def queryset_manager(qs_class): class Manager(QuerySetManager): queryset_class = qs_class return Manager()
Now it's enough to write:
objects = queryset_manager(MyQuerySet)
In UNIX way, each view should solve single task. This is good idea, but sometimes we need to mix logic of different views on same page. Filter, sort, paginate, or, for example, add comment on product page. In this article I'll show how we can mix such multiple views.
Django ORM is a very abstract and flexible API. But if you do not know exactly how it works, you will likely end up with slow and heavy views, if you have not already. So, this article provides practical solutions to N+1 and high loading time issues. For clarity, I will create a simple view that demonstrates common ORM query problems and shows frequently used practices.
When dealing with one of our projects (LookSMI media monitoring platform) we have to handle the huge volume of data – and its quantity is constantly growing. At the same time, we must run quick searches with smart rules. In this article I'll explain how we have achieved required performance.