Django: Model inheriting from base model with custom manager. Can the manager have dynamic variables?
I need to have all my models inherit this manager (Haven't tested this manager and it may be ridiculous so I'm completely open to suggestion/criticism on it as well):
class AccountFilterManager(models.Manager):
def __init__(self, account=None, *args, **kwargs):
super(AccountFilterManager, self).__init__(*args, **kwargs)
self.account = account # account of course will be an instance of Account(models.Model)
def get_query_set(self):
if self.account:
return super(AccountManager,self).get_query_set().filter(account=self.account)
You can see what I'm trying to do. limit the need to filter out everywhere based on what account I'm dealing with.
What would be the best way to get this manage开发者_如何学Gor to work with all my models? Abstract base model with it? Also, how am I going to pass in the account variable into it from the view level? Is this all wrong and evil? I've been trying to find a way to conquer this for a week now :(.
No. The manager is instantiated as a class attribute, thereby giving all model instances the same manager.
精彩评论