salezica
salezica

Reputation: 76929

Django: user profile creation forms

As I've understood the deal, the standard way of extending the User Django model is by implementing profiles.

This suits me wonderfully, since I have 3 types of profiles (3 different Model subclasses) all based upon the User model for uniqueness and authentication.

I'm having trouble deciding, however, on the best way of implementing a user creation form. Should I...

  1. Use mixins: inherit a ModelForm with model=User and one with model=Profile, manually overriding save() and clean() to call the bases' methods and perform the foreign key assignment.

  2. Use composition: inherit Form and manually instantiate and manage two ModelForms.

  3. Instantiate the forms separately, and save() them in a manually ordered fashion.

  4. The Best Way I Didn't Think Of.

I would appreciate a (however minimal) implementation so I can be sure I'm making the right calls.

Thanks in advance.

Upvotes: 0

Views: 408

Answers (1)

Chris Lawlor
Chris Lawlor

Reputation: 48902

You can write a single form that acts as a combined ModelForm for both your Profile model and the contrib User model. To accomplish this, a little manipulation of the forms kwargs on init is required. See this snippet: http://djangosnippets.org/snippets/2081/

Upvotes: 1

Related Questions