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?
1 Answer 1
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.