DJANGO complex modelling

感情迁移 提交于 2019-12-13 03:02:57

问题


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:


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



来源:https://stackoverflow.com/questions/2942458/django-complex-modelling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!