Django 建模

发布于 2024-09-05 04:41:45 字数 1081 浏览 12 评论 0原文

概念:

饮料是由成分制成的。例如10毫升伏特加。有些收据的成分非常特殊(10 毫升芬兰伏特加),有些则不然(10 毫升任何伏特加)。

我想知道如何对组件进行建模来解决这个问题 - 我有库存特定产品,它可以满足更多要求。

现在的模型是:

class Receipt(models.Model):
  name = models.CharField(max_length=128)
  (...)
  components = models.ManyToManyField(Product, through='ReceiptComponent')

  def __unicode__(self):
    return self.name

class ReceiptComponent(models.Model):
  product = models.ForeignKey(Product)
  receipt = models.ForeignKey(Receipt)
  quantity = models.FloatField(max_length=9)
  unit = models.ForeignKey(Unit)
  class Admin:
    pass
  def __unicode__(self):
    return unicode(self.quantity!=0 and self.quantity or '') + ' ' + unicode(self.unit) + ' ' + self.product.genitive

class Product(models.Model):
  name = models.CharField(max_length = 128)
  (...)
  class Admin:
    pass
  def __unicode__(self):
    return self.name

class Stock(Store):
  products = models.ManyToManyField(Product)
  class Admin:
    pass
  def __unicode__(self):
    return self.name

我考虑制作一些表格,将真实产品(库存)与抽象产品(收据组件)连接起来。但也许有简单的解决方案?

Concept:

Drinks are made of components. E.g. 10ml of Vodka. In some receipt the component is very particular (10ml of Finlandia Vodka), some not (10 ml of ANY Vodka).

I wonder how to model a component to solve this problem - on stock I have particular product, which can satisfy more requirements.

The model for now is:

class Receipt(models.Model):
  name = models.CharField(max_length=128)
  (...)
  components = models.ManyToManyField(Product, through='ReceiptComponent')

  def __unicode__(self):
    return self.name

class ReceiptComponent(models.Model):
  product = models.ForeignKey(Product)
  receipt = models.ForeignKey(Receipt)
  quantity = models.FloatField(max_length=9)
  unit = models.ForeignKey(Unit)
  class Admin:
    pass
  def __unicode__(self):
    return unicode(self.quantity!=0 and self.quantity or '') + ' ' + unicode(self.unit) + ' ' + self.product.genitive

class Product(models.Model):
  name = models.CharField(max_length = 128)
  (...)
  class Admin:
    pass
  def __unicode__(self):
    return self.name

class Stock(Store):
  products = models.ManyToManyField(Product)
  class Admin:
    pass
  def __unicode__(self):
    return self.name

I think about making some table which joins real product (on stock) with abstract product (receiptcomponent). But maybe there's easy solution?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

压抑⊿情绪 2024-09-12 04:41:45

我想我会采用一种更复杂的方法,使用树结构,其中 Product 对象位于层次结构中。可能有一个名为“酒精”的节点,其子节点为“伏特加”、“威士忌”、“啤酒”。而“伏特加”有子节点“芬兰伏特加”和“俄罗斯伏特加”,

如果库存中没有“完成伏特加”,首先检查其所有子节点(“绝对伏特加”,...),然后遍历其兄弟节点(“俄罗斯伏特加”) vodka”),然后是其父节点(按相反顺序)(“vodka”、“alcohol”),直到找到有库存的节点。 num_in_stock 将是产品表中的整数字段。

google code http://code.google.com/p/django-mptt/ 这对于 django 中的树来说非常有用。

I think I'd go with an even more complicated approach using a tree-structure where Product objects are in a hierarchy. There might be a node called "alcohol" with child nodes "vodka", "whisky", "beer". And "vodka" has child nodes "Finnish vodka" and "Russian vodka"

If there is no "Finish vodka" on stock, first check all its children ("Absolut vodka", ...), then traverse its siblings ("russian vodka"), and then its parent nodes (in reverse order) ("vodka", "alcohol") until one is found that is on stock. num_in_stock would be an integer field in the product table.

There is a well known and great working app called mptt (Modified Pre-ordered Tree Traversal) on google code http://code.google.com/p/django-mptt/ which is great for trees in django.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文