Hi all,
I need to show a user only the objects that he owns. Since I need to do this on more then 80% of my views, hardcoding this kills DRY. More so, it is absolutely imperative that a user never sees records owned by others. And doing it by hand (in all the views) also seems error prone.
I've been looking at decorators (like login_required) but that seems to early in the request handling process. Is it possible to get the request.user value into a custom written manager and do something like this:
class CustomerManager(models.Manager):
def get_query_set(self):
return super(CustomerManager, self).get_query_set().filter(created_by=request.user)
Or is the manager object just as 'no go!' as the model definition as far as request info is concerned?
Thanx a lot.
This is the saving objects part, but purely here as elaboration and not a necessary read.
The saving part is relatively secure. I changed the object.save() function on the model so it takes the userid as a parm.
def save(self, userid):
self.created_by = userid
super(Customer, self).save(userid)
In the view:
if form.is_valid():
customer = form.save(commit=False)
customer.save(request.user)
This way I dont need to have the line below in my view before customer.save...
customer.created_by = request.user
And thus making it less error prone.