【问题标题】:Design Django's models using ManyToManyField for multivendor scheme?使用 ManyToManyField 为多供应商方案设计 Django 模型?
【发布时间】:2012-08-17 21:20:50
【问题描述】:

我有一个 Item 模型,尊重 Owner,每个 item 可以有多个 owner,每个 owner 可以有多个 item。如下:

class User(models.Model):
    user = models.ForeignKey(DjangoUser)

class Item(models.Model):
    owners = models.ManyToManyField(User, through='**ItemOwner**')

class ItemOwner(models.Model): 
    item = models.ForeignKey(Item)
    owner = models.ForeignKey(User)

    class Meta(models.Model.Meta):
        db_table = 'items_owners'

我还有一个类 PricePremiumPrice 来设置商品的价格:

class **Price**(models.Model):
    price = models.DecimalField(max_digits=12) 

class **PremiumPrice**(models.Model):
    item = models.OneToOneField(Item)
    price = models.ForeignKey(Price)

如您所见,每个物品只能有一个由 PremiumPrice 类设置的价格,每个物品都归该物品的所有者所有,任何所有者都可以更改价格,但价格是唯一的物品。此外,当有人购买该商品时,它由 PurchaseItem 类处理,如下所示:

class PurchaseItem(models.Model):
    item = models.ForeignKey(Item)
    user = models.ForeignKey(User)

    class Meta:
        db_table = 'purchase_item'
        unique_together = ('item', 'user')

现在,我想将其转换为多供应商方案。每个项目可以由多个所有者拥有,每个所有者都可以为自己的项目设置自己的价格。 所以我认为我需要做的是将价格添加到项目模型并创建一个新类 ItemPrice (为每个项目添加价格):

class Item(models.Model):
    owners = models.ManyToManyField(User, through='ItemOwner')
    prices = models.ManyToManyField(Price, through='ItemPrice')

class ItemPrice(models.Model): 
    item = models.ForeignKey(Item)
    price = models.ForeignKey(Price)
    class Meta(models.Model.Meta):
        db_table = 'items_prices'

然后将类 PremiumPrice: 项目从 OneToOneField 更改为 ForeignKey,还包括所有者:

    class PremiumPrice(models.Model):
        item = models.ForeignKey(Item)
        price = models.ForeignKey(Price)
        owner = models.ForeignKey(User)

为了记录每笔交易,PurchaseItem 类还需要包含所有者,unique_together 还需要新值:

class PurchaseItem(models.Model):
    item = models.ForeignKey(Item)
    user = models.ForeignKey(User)
    owner = models.ForeignKey(User)

    class Meta:
        db_table = 'purchase_item'
        unique_together = ('item', 'user', 'owner') #

但我仍然不确定我是否正确。因此,如果您对我可能遇到的错误/陷阱有任何 cmets/建议,请告诉我。我真的很感激。

非常感谢!

【问题讨论】:

    标签: django django-models many-to-many


    【解决方案1】:

    您的模型似乎比实际需要的要复杂一些。你到处都在使用through=,但实际上并不是必需的,除非你是adding extra fields to a many-to-many relationships

    假设您的要求是描述的而不是编码的,您可以将其归结为:

    class User(models.Model):
        user = models.ForeignKey(DjangoUser)
        owned_items = models.ManyToManyField(Item, through='ItemSale')
        purchased_items = models.ManyToManyfield(Item, related_name='purchasers')
    
    class Item(models.Model):
        pass
    
    class ItemSale(models.Model):
        owner = models.ForeignKey(User, related_name='owners')
        item = models.ForeignKey(Item)
        price = models.DecimalField(max_digits=12)
    

    您仍然可以访问项目的ownersprices(后者通过查看相关的ItemSale 对象)。

    【讨论】:

    • 谢谢 supervacuo。事实上,这是我从一个非常结构化且经过优化的大型项目中提取的内容,像 Item & User 这样的类有近 60 个字段,很多东西紧密结合在一起。所以我认为我不能只在代码中添加 2 个字段owned_items 和 purchase_items 并且它会起作用,因为它通常会破坏结构。我上面的代码我觉得没有那么复杂,我所做的只是展示我的思考过程和我的变化,所以阅读起来有点长。
    • 啊,我想知道你是否从一个更大的项目中提取了它。不过,我认为我的观点仍然存在——你不需要PurchaseItemPremiumPriceItemPrice ,除非你实际上是在存储信息(除了ForeignKeys 到相关的对象)。
    • 我意识到这可能不是您想听到的,但我建议您将数据表示更改为不那么复杂(我的 12 行代码与您的 ~3 打代码相同,并且少 3 个数据库表)。
    • 如果您这样做,为您的Item 对象添加每个所有者的价格是您第一次需要中间模型。如果没有,我的回答可能对你没有帮助,对不起。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2020-05-11
    • 2020-04-20
    相关资源
    最近更新 更多