DJANGO复杂建模 [英] DJANGO complex modelling

查看:110
本文介绍了DJANGO复杂建模的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有这样的模式:收据包含组件。组件包含产品。



组件和产品之间的区别是该组件具有数量和度量单位:
eg。组分是100g糖 - 糖是一种产品。



所以我需要制作很多组分来满足不同的食谱 - 100g糖不等于200g糖



我想知道我是否可以重构它来启动组件 - 在纯sql中,这很简单,但是我正在尝试使用django,而不是解决办法。

 类收据(models.Model):
name = models.CharField(max_length = 128)
(...)
components = models.ManyToManyField(Component)

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

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

TIA

解决方案

如果您在收据模型中使用通过使用ManyToMany关系,则可以摆脱组件模型:
http://docs.djangoproject.com/en/1.2/topics/db/models/#intermediary-manytomany


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

解决方案

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

这篇关于DJANGO复杂建模的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆