views:

89

answers:

1

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 that 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?

+2  A: 

Use an explicit through table. See the documentation.

Daniel Roseman
Thanks for the reply. I think that makes sense, but can you explain how I can be sure that each price will be unique for each store?
buken
This should be evident from the docs example. You implement an intermediary table/Model and for any given combo of Store/Item, you can have additional data such as price (you might want to remove the `price` field from the `Item` model).
Rishabh Manocha
If you need to ensure uniqueness of a price for a given store (seems like an odd requirement, frankly), you'd use the unique_together Meta option on the through model, to ensure that ('store', 'price') are unique_together.
Carl Meyer
Thanks for the comments guys. I was able to do this after reading the documentation. I shouldn't have said the price should be unique for each store. What I meant was that it should be specific to each store, and not general for all stores.
buken