开发者

Django: how to properly use UserProfile? (Django auth extra information)

开发者 https://www.devze.com 2023-04-09 15:45 出处:网络
i finally can make my Django+Auth app works. I add the extra information to the user as the docs say开发者_开发知识库. Now, i\'ve a simple question. When i\'m building a model that is related to the u

i finally can make my Django+Auth app works. I add the extra information to the user as the docs say开发者_开发知识库. Now, i've a simple question. When i'm building a model that is related to the user, which user should i relate to? To auth.models.User or to my accounts.UserProfile?

An example: I've a model for Product, and the Product belongs to a user. Which would be the best option:

class Product(models.Model):
    user = models.ForeignKey(auth.models.User)

or

class Product(models.Model):
    user = models.ForeignKey(accounts.UserProfile)

I'm currently using auth.models.User, becouse i can issue a get_profile, but some friend told me that i was wrong.

Thank you!


You're doing the right thing.

UserProfile is just an extension of the User model. And logically you're making a relation of a object with an object, not a relation of a object with some extra information.

Also, as you mentioned, you can always issue the 'get_profile' to get the extra data.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号