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.
We have to update QuerySetManager in such a way, that will allow setting queryset as class field:
In order to use this manager, we have to create a sublcass and set queryset_class:
It's not fun to create such class every time. So we'll write a function that does it for us:
Now it's enough to write:
Usually users' profiles are stored in single model. When there are multiple user types, separation is made by some field like user_type.Situation is a little more complicated when different data is needed for each user type.In this article I'll describe how I solve this task.
Welcome all who are reading this article. I was given a task of creating a parser (spider) with the Scrapy library and parsing FTP server with data. The parser had to find lists of files on the server and handle each file separately depending on the requirement to the parser.