目前,拥有一个包含Item表和Stock表的数据库。两者之间存在着多对多的关系。单个项目对象可以具有多种大小。下一步是为每个尺寸的项目分配一个“inStock”选项。 关于实现这一点的任何想法?
当前models.py
class Stock(models.Model):
size = models.CharField(max_length=30)
stock = models.BooleanField(default=False)
def __str__(self):
return self.size
class Item(models.Model):
title = models.CharField(max_length=250, null=True, unique=True)
price = models.DecimalField(max_digits=8, decimal_places=2)
aw_product_id = models.CharField(max_length=11, null=True) # Removed because multiple products has similar identifer
url = models.URLField(max_length=250) # Removed 'unique=True'as the aw_prod_id will throw an integrity error
image = models.CharField(max_length=1000)
retailer = models.CharField(max_length=250)
category = models.CharField(max_length=100)
featured = models.CharField(max_length=10, default='NO')
gender = models.CharField(max_length=100, null=True)
sizes = models.ManyToManyField(Stock)
uniq_id = models.CharField(max_length=11, null=True, unique=True) # Removed because multiple products has similar identifer
def __str__(self):
return self.title
答案 0 :(得分:1)
您可以使用ManyToManyField的through
参数来指定用于关系的另一个模型,以及其他字段,而不是django默认创建的自动生成模型。