I have users creating lists in my site and I’m using the generic CreateView to allow them create them. One of the fields of a list is the owner (who creates it).
It does not make sense to ask users to select the owner, since the user that is creating it is already the owner.
So, I’m passing to the CreateView a “form_class” instead of a “model” parameter where the form_class excludes the owner field.
In order to save the form, I need to add the owner to it.
I subclassed CreateView and added a form_valid method, but I’m having problems doing this.
So far, I got
class MyCreateView(CreateView)
form_class = ListForm
def form_valid(self, form):
form.??? = self.request.user -->> expression of the left side
return super(MyCreateView, self).form_valid(form)
It should be something simple. In this thread, they say to use self.object.owner, but self.object is a None type.
How should I handle this?
Thanks
You’re looking for
form.instance.owner = request.userand then call super