DJANGO complex modelling

2019-08-24 08:02发布

I have such model now: receipt contains components. component contain product.

The difference between component and product is, that component has quantity and measure unit: eg. component is 100g sugar - sugar is a product.

So I need to make lots of components to satisfy different recipes - 100g sugar is not equal 200g sugar

I wonder if I can remodel it to kick off components - in pure sql it's rather easy, but I'm trying to USE django - not making workarounds.

class Receipt(models.Model):
  name = models.CharField(max_length=128)
  (...)
  components = models.ManyToManyField(Component)

class Component(models.Model):
  quantity = models.FloatField(max_length=9)
  unit = models.ForeignKey(Unit)
  product = models.ForeignKey(Product)

class Product(models.Model):
  name = models.CharField(max_length = 128)

TIA

1条回答
一夜七次
2楼-- · 2019-08-24 08:26

You can get rid of the Component model if you use a ManyToMany relationship using "through" in your Receipt model: http://docs.djangoproject.com/en/1.2/topics/db/models/#intermediary-manytomany

查看更多
登录 后发表回答