0

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?

asked Jun 9, 2010 at 21:01

1 Answer 1

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.

answered Jun 9, 2010 at 21:42
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.