Django ModelForms and request.user

Took a little bit of digging to figure this one out. If you’re using a ModelForm along with django.contrib.auth.models.User (or a customized User model), you’ll want to use the instance that is sitting in the request rather than allowing the ModelForm to create a new instance.

To do this, simply override the get_form_kwargs method in the view:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s