Answer the question
In order to leave comments, you need to log in
How to globally filter a Django model?
Hello everyone,
our poster site www.yaidu.ru is written in Django. Obviously, there is a lot of code that works with the Event model (event). Not in the programmer's sense of event :), but as an event.
Site, historically, Irkutsk. Now we have a task to expand it to other cities, which means that the event model has a new city attribute - a city. Initially, it was not provided.
There will be a global parameter like "current city", which will set the middleware. Is it possible to additionally filter all selections in the event model by this parameter, without rewriting the code of the entire site, but somewhere in one place? And what is this place, if any?
Answer the question
In order to leave comments, you need to log in
I would try to rewrite the objects manager in the models in this case.
First, make an inherited and rewritten manager class with a filter, assign it to a variable, and then assign it to all models.
There is an example at the very end of djbook.ru/rel1.4/topics/db/managers.html - only without mgr.use_for_related_fields = True you need to do it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question