简体   繁体   中英

Foreignkey of Django User model to another model

I know how to define user profile model in django as follows.

class Profile(models.Model):
    user = models.OneToOneField(User)
    job_title = models.CharField(...)

But, if I have created an organisation model, and will let multiple users belong to a certain organization, then how to modify the Django User model to that one organization has multiple users?

创建一个扩展用户模型的新模型,然后在其中具有名为organisation的外键属性

There is no need to adjust the User model in any way. You have user profiles, each of which is tied to the respective user, so use your user profiles now:

class Organization(models.Model):
    ...

class Profile(models.Model):
    ...
    organization = models.ForeignKey(Organization)

And then you can perform all lookups via Profile model. And if you need real user object, you can always get it as profile.user .

You can still customize your user model if you really want to of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM