Search code examples
djangodjango-modelsdjango-authenticationdjango-users

Cannot assign - must be a "UserProfile" instance


I have a class UserProfile defined which takes the default user as a foreign key. Now another class A has a foreign key to UserProfile.
So for saving any instance in class A, how do i give it the userprofile object.

Also, does making a class UserProfile mean that class user is still used and class UserProfile is just some other table?
I need to know this as I have to take care of the user profile creation, so I should know what gets stored where?

-- Confused


Solution

  • So for saving any instance in class A, how do i give it the userprofile object.

    1. Create a app with a model which has a models.OneToOneField(User) or a models.ForeignKey(User, unique=True).
    2. Make your project aware of your UserProfile by pointing to it from the settings.py file AUTH_PROFILE_MODULE = 'myapp.UserProfile'.
    3. Read the documentation.

    Also, does making a class UserProfile mean that class user is still used and class UserProfile is just some other table?

    Yes, your database will have both a auth_user and a user_profile table. This is due to the fact that using UserProfiles doesn't mean all user have to have profiles. Only the additional fields defined in the UserProfile model will be in the user_profile table.

    I need to know this as I have to take care of the user profile creation, so I should know what gets stored where?

    James Bennett created two nice apps which with a few hours of careful reading will be of great help especially when it comes to the user registration part. Go look at django-registration and django-profiles.