Search code examples
pythondjangoregistrationdjango-registration

How to fields to the User model


I want to be able to save a picture to a user and maybe even a description, these can be optional, but how to you add to a model. I don't want to go into the souce code of django to change it, can I overide a model to add fields, if so how?

how do i access these accounts.UserProfile and accounts.UserProfile.image in my views.py file

I have django-registration installed


Solution

  • From the docs:

    If you'd like to store additional information related to your users, Django provides a method to specify a site-specific related model -- termed a "user profile" -- for this purpose.

    To make use of this feature, define a model with fields for the additional information you'd like to store, or additional methods you'd like to have available, and also add a OneToOneField named user from your model to the User model. This will ensure only one instance of your model can be created for each User. For example:

    from django.contrib.auth.models import User
    
    class UserProfile(models.Model):
        # This field is required.
        user = models.OneToOneField(User)
    
        # Other fields here
        accepted_eula = models.BooleanField()
        favorite_animal = models.CharField(max_length=20, default="Dragons.")
    

    To indicate that this model is the user profile model for a given site, fill in the setting AUTH_PROFILE_MODULE with a string consisting of the following items, separated by a dot:

    1. The name of the application (case sensitive) in which the user profile model is defined (in other words, the name which was passed to manage.py startapp to create the application).
      1. The name of the model (not case sensitive) class.

    For example, if the profile model was a class named UserProfile and was defined inside an application named accounts, the appropriate setting would be:

    AUTH_PROFILE_MODULE = 'accounts.UserProfile'