Django Updateview Generic Class
How can I access the object passed by the user inside a generic view class? In template, when the user clicks the link:
Solution 1:
classUpdatePeon(generic.UpdateView):
if item['attr1'] == "Attribute1":
model = model1form= model1Form
else:
...
You can't put code in the class body like this. It runs when the module is loaded, before you have access to the request
.
You should override a specific method. For example you can override get_form_class
to change the form class used by the view. Inside the view, you can access the object being updated with self.object
.
classUpdatePeon(generic.UpdateView):
defget_form_class(self):
if self.object.pk == 1:
return MyForm
else:
return OtherForm
You may find the ccbv website useful for exploring the update view methods.
Post a Comment for "Django Updateview Generic Class"