views:

194

answers:

2

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

+1  A: 

I assume your UserProfile model is intended to store additional information about your users. If so, there's documentation about the best approach to do this, which in brief is:

  • 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 from your model to the User model. This will ensure only one instance of your model can be created for each User.
  • Set AUTH_PROFILE_MODULE to myapp.MyModel, where myapp is the app containing the model MyModel which you want to use to store extra information about your users.
Dominic Rodger
Yes, I have read that:Two things - 1. Does this stop using the `class user` (I have used a foreign key to the User model) 2. Any other model which might require user information should ahve a foreign key just to the `class user` will this work? Or will the new model i set in AUTH_PROFILE_MODULE will take over? I hope I am clear. Thanks.
zubinmehta
+1  A: 

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.

Simon Zimmermann
Thanks Simon. I just have one small doubt - So this means that the user auth can still be done using the normal user class as foreign key wherever required and I should not use the UserProfile class except when I am concerned with the "profiles"!
zubinmehta
I think that this is a design question. If ALL users of your project have a profile you could always import the UserProfile, but I guess it's more common to only import the User to create reusable apps. In the first option your UserProfile becomes a dependency to all other apps and that might not be desirable.
Simon Zimmermann