django modeling

Posted by SledgehammerPL on Stack Overflow See other posts from Stack Overflow or by SledgehammerPL
Published on 2010-06-09T21:01:30Z Indexed on 2010/06/09 22:02 UTC
Read the original article Hit count: 198

Filed under:
|
|

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?

© Stack Overflow or respective owner

Related posts about django

Related posts about modeling