简体   繁体   English

Django ManyToMany通过关系

[英]Django ManyToMany Through Relationship

I have a following criteria 我有以下标准

where I have User Model, which itself extended was from Django user model. 我有用户模型,该模型本身是从Django用户模型扩展而来的。

Class User
   name = models.CharField()
   ...

A user can become a seller when he is approved by the admin. 用户在获得管理员批准后可以成为卖方。 Seller model goes like this. 卖方模型是这样的。

Class Seller
    location = models.CharField()
    INDIVIDUAL = '1'
    COMPANY = '2'
    ACCOUNT_CHOICES = (
        (INDIVIDUAL, 'individual'), 
        (COMPANY, 'company'),
    )

    account_type_name = models.CharField(max_length=2,choices=ACCOUNT_CHOICES, default=INDIVIDUAL)

And Seller can belong to any of the second or third level categories 卖方可以属于任何第二或第三级类别

Class Level1Category
    name = models.CharField()

Class Level2Category
    name = models.CharField()
    level1 = models.ForeignKey(Level1Category)

Class Level3Category
    name = models.CharField()
    level2 = models.ForeignKey(Level2Category)

When a user apply for seller account he has to select any one of the level 2 or level 3 category. 用户申请卖方帐户时,他必须选择2级或3级类别中的任何一个。 Which would be the efficient model architecture for this. 这将是有效的模型架构。 How can I link the category model with the seller and also link these two models with the user. 如何将类别模型与卖方链接,以及如何将这两个模型与用户链接。

EDIT 编辑

My User Model is already a extended version of Django User Model. 我的用户模型已经是Django用户模型的扩展版本。 I'm doing that because I have two different type of profiles, one is seller and the other is buyer. 我这样做是因为我有两种不同类型的配置文件,一种是卖方,另一种是买方。

I would suggest you try by extending Django's User model ( see docs ) 我建议您尝试通过扩展Django的用户模型来尝试( 请参阅docs

Your seller would extend from your User model: 您的卖方将从您的用户模型扩展:

class User(models.Model):
    name = models.CharField()
    ....

class Seller(models.Model):
   user = models.OneToOneField(User, on_delete=models.CASCADE)
   location = models.CharField()
   ...

You could create a ManyToMany relationship for your categories ( see docs ): 您可以为您的类别创建ManyToMany关系( 请参阅docs ):

class Category(models.Model):
    name = models.CharField()
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    ...

Or one thing that come to mind, you maybe could extend what you have done for your account_type_name: 或想到的一件事,您可能可以扩展为account_type_name做的事情:

LEVEL_CATEGORY = (
    (1, 'Level 1'), 
    (2, 'Level 2'),
    (3, 'Level 2'),
)

level_category = models.CharField(choices=LEVEL_CATEGORY, default=1)

But I am not sure if this is really what you want to do. 但是我不确定这是否真的是您想要的。

If you need just basic user field such as; 如果只需要基本的用户字段,例如; name, email etc, you can extend Django's User model as @HendrikF suggest then; 名称,电子邮件等,您可以按照@HendrikF的建议扩展Django的用户模型;

class Seller(models.Model):
   user = models.OneToOneField(User, on_delete=models.CASCADE)
   location = models.CharField()
   ...

if you need customize User model I do suggest you Django AbstractUser for example; 如果您需要自定义用户模型,我建议您例如使用Django AbstractUser。

from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    consumer = models.ForeignKey('Consumer', null=True, blank=True)
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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