问题
I would like my models to automatically filter by current user.
I did this by defining:
class UserFilterManager(models.Manager):
def get_queryset(self):
return super(UserFilterManager, self).get_queryset().filter( owner=get_current_user() )
where get_current_user()
is a middleware which extracts the current user from the request
passed to Django.
However, I need to use the models from Celery which does not go through the middleware. In these cases
MyModel.objects.all()
needs to become
MyModel.objects.filter(user=<some user>)
To avoid wrong queries caused by forgetting to filter by user, I would like the model/manager/queryset to assert when a query (any query) is performed without a filter on user
.
Is there a way to achieve this?
From what I see get_queryset()
cannot receive parameters and models.QuerySet
won't provide aid here.
来源:https://stackoverflow.com/questions/41747344/django-custom-manager-filter-query-set-by-parameter