Back
Sep 23, 2010

Dynamic class generation, QuerySetManager and use_for_related_fields

Vladimir Sidorenko

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:

false

Now we need to make it work for related objects:

user.articles.old().public()

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.

false

We have to update QuerySetManager in such a way, that will allow setting queryset as class field:

false

In order to use this manager, we have to create a sublcass and set queryset_class:

false

It's not fun to create such class every time. So we'll write a function that does it for us:

false

Now it's enough to write:

false
More thoughts