Wednesday, November 30, 2016

Django: UpdateView - dispatch() : reduce the number of queries


I have a Product Model, foreign key to Company Model, onetoone key to User.

I override the dispatch(method) to check if the user has rights to edit the object(product).

I'm trying to optimize the queries to the database because some of them are duplicates.

def dispatch(self, request, *args, **kwargs):
    obj
= self.get_object()
   
if obj.company.user != request.user:
       
raise PermissionDenied
   
else:
       
print('ok')
       
return super().dispatch(request, *args, *kwargs)



query to products

obj = self.get_object()



query to company and query to user twice

if obj.company.user != request.user:



query to products again

 return super().dispatch(request, *args, *kwargs)



How can I optimize and remove duplicated queries ?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/725caa3d-4686-418a-b947-db4be442f0fa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment