HardCodeStuds
HardCodeStuds

Reputation: 539

Django - extra fields for user form

In the site I'm developing I have a form to register users that extends the userCreationForm. Said form only adds fields that are not in the default, but they are in the model such as email, first_name and last_name.

class RegisterForm(UserCreationForm):
    email = forms.EmailField(label = "Email", required=True)
    first_name = forms.CharField(label = "First name", required = True )
    last_name = forms.CharField(label = "Last name", required = True)

    class Meta:
        model = User
        fields = ("username", "first_name", "last_name", "email", "password1", "password2" )

    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)

        for fieldname in ['username', 'password1', 'password2']:
            self.fields[fieldname].help_text = None

My question is if there's a way to add another field like an imagefield, date of birth, etc. so that I can make a more complete user profile? Or do I have to create my own user model?

Upvotes: 0

Views: 755

Answers (1)

Aaron Lelevier
Aaron Lelevier

Reputation: 20848

The Django idiomatic way of adding more fields to the User model is to make a second model where User is a ForeignKey. So:

from django.db import models
from django.contrib.auth.models import User

class MyUser(models.Model):
    user = models.ForeignKey(User)
    # ... other fields.  i.e. imagefield, date of birth (dob), etc ..
    # example:
    dob = models.DateField()

Upvotes: 1

Related Questions