简体   繁体   中英

how to resolve foreign key relations to user model in django

Consider the django auth User model inherited to create the users.

class UserProfile(models.Model):
  user = models.OneToOneField(User)

class Domain(models.Model):
  customer = models.ForeignKey(User)

class EmailAccount(models.Model):
  domain = models.ForeignKey(Domain)

I wish to create more than 1 user for the given UserProfile instance(User profile is a customer that should be stored only once); if another user is created, he will be able to manage the particular domain only and hence the email accounts created on that domain. He should not be able to access any email accounts created on any other domain for the same user once he logs in. I wish to use the django admin to do the same.

Many-to-many relationships allow many on either side.

class UserProfile(models.Model):
    Users = models.ManyToMany(User)

>>> u1 = User(id=1)
>>> u2 = User(id=2)
>>> user_profile = UserProfile()
>>> user_profile.save()
>>> user_profile.users.all()
[]
>>> user_profile.users.add(u1.id, u2.id)
>>> user_profile.save()
>>> user_profile.users.all()
[<User: User object>, <User: User object>]

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