Skip to content

Instantly share code, notes, and snippets.

@sleekslush
Created January 24, 2012 02:23
Show Gist options
  • Save sleekslush/1667396 to your computer and use it in GitHub Desktop.
Save sleekslush/1667396 to your computer and use it in GitHub Desktop.
Adding request user to a form save
class MyView(CreateView):
model = Team
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.user = self.request.user
self.object.save()
return FormMixin.form_valid(self, form)
@dnoyes
Copy link

dnoyes commented Jan 24, 2012

Great explanation. Thanks.

To avoid severe brain melt, I've backed off an am doing it the "easy" way first. I'll post that when I've got it done (probably tomorrow night). Then I can circle back to try and use CreateView again.

Even without CreateView, I think it's going to be pretty slim. Being able to use ModelForm and form.save(commit=False) makes things super nice.

@dnoyes
Copy link

dnoyes commented Jan 25, 2012

Here's what I ended up with: https://gist.github.com/1674741

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment