I have two generic class based views, CreateView and UpdateView.  

I have products and companies, where a company can have multiple products. 
A user has a company.

When a product is added I need to send also the corresponding foreign key 
and when I edit/update I need to be sure that the product update belongs to 
the company of the user.

Code:

1.       Create

*def *form_valid(self, form):
    company = self.get_company()
    form.instance.company = company
    valid_data = super(ProductCreateView, self).form_valid(form)
    *return *valid_data

 

2.  Update

 

*def *get_object(self, *args, **kwargs):
    company = self.get_company()
    obj = super(ProductUpdateView,self).get_object(*args, **kwargs)
    *if *obj.company == company:
        *return *obj



 

Why for create the instance is use (form.instance.company = company) and on 
update the object is requested ? (I have general knowledge about OOP)

-- 
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+unsubscr...@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/5100154c-5480-41e1-9aee-8e13b39d9520%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to