开发者

Unique field values and ManyToMany relationships

开发者 https://www.devze.com 2022-12-11 23:56 出处:网络
Let\'s say I have a class structure that is defined below: Class Item(models.Model): ... price = models.IntegerField()

Let's say I have a class structure that is defined below:

Class Item(models.Model):
    ...
    price = models.IntegerField()
    upc = models.IntegerField()
    ...

Class Store(models.Model):
    ...
    inventory = models.ManyToManyField(Item)
    ...

Basically I want store models to have access to the same inventory. However the value of price in the item model will be unique for each store tha开发者_JS百科t links to it. e.g. I might have an instance of the item model called bike that all stores will have access to. For all the stores the upc (barcode) will be the same, but the price will be different for each store. Is there any way to implement that relationship using this class structure?


Use an explicit through table. See the documentation.

0

精彩评论

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