Reputation: 1412
I'm trying to make inventory system for products like Tyre/Tube.
There are many categories like Truck Nylon, Jeep Nylon, Jeep Radial, Car Nylon, Car Radial etc. Under each category, there will be many products with specification names like, 825.20.16, 900.20.14, 135/70R12 etc.
For every specifications, there will be different manufactures. Some specifications under Radial type tyres may have two variants Tubed Tyre & Tubeless Tyres.
Here's my models.
class Category(MPTTModel):
name=models.CharField(max_length=75,null=False,blank=False, unique=True)
parent=TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)
def __str__(self):
return self.name
PRODUCT_TYPE=(('TL','Tubeless Tyre'), ('TT','Tubed Tyre'), ('NA','Not applicable'))
class Product(models.Model):
name = models.CharField(max_length=30,null=False, blank=False)
category=TreeForeignKey(Category, null=False,blank=False)
def __str__(self):
return '%s = %s' % (self.name,self.category.name)
class Meta:
ordering=['category']
**unique_together = ('name', 'category')**
class ProductStock(models.Model):
product=models.ForeignKey(Product,null=False,blank=False)
manufacturer=models.OneToOneField(Manufacturer, null=False,blank=False)
product_type=models.CharField(max_length=2, choices=PRODUCT_TYPE,)
opening_stock=models.PositiveIntegerField(default=0)
def __str__(self):
return '%s (%s) stock = %d ' % (self.product, self.manufacturer, self.opening_stock)
class Meta:
ordering=['manufacturer']
**unique_together = ('product', 'manufacturer','product_type')**
Please see the last unique_together. I need to ensure that there won't be any duplicate Tubeless or Tubed-tyre for any manufacturer under any specification.
I have added a stock as given below.
> Car Radials => 135/70R12
> Manufacturer: CEAT
> Type: Tubeless Tyre
> stock: 5
Now, when I try to enter a Tubed-tyre type stock for the above set of parameters, (Car Radials => 135/70R12 , Manufacturer: CEAT , Type : Tubed tyre, Stock : 5) I get the following error.
Product stock with this Product, Manufacturer and Product type already exists.
There's only a single entry in the product_stock table.
I can't figure out what going wrong here. Any help would be highly appreciated.
Thanks
PS: Having some trouble in using code tags - sorry.
Upvotes: 2
Views: 1673
Reputation: 27553
change this line
manufacturer=models.OneToOneField(Manufacturer, null=False,blank=False)
to
manufacturer=models.ForeignKey(Manufacturer, null=False,blank=False)
Upvotes: 2