model.py:
from django.db import models
class Order(models.Model):
name = models.CharField(max_length=64)
def __unicode__(self):
return self.name
class Product(models.Model):
name = models.CharField(max_length=64)
def __unicode__(self):
return self.name
class SubProduct1(Product):
prop1 = models.FloatField()
def __unicode__(self):
return self.name
class SubProduct2(Product):
prop2 = models.EmailField()
def __unicode__(self):
return self.name
class OrderProduct(models.Model):
order = models.ForeignKey(Order)
product = models.ForeignKey(Product)
amount = models.IntegerField()
def __unicode__(self):
return u'%s - %s - %s' % (self.order.name, self.product.name,
self.amount)
Now it's trivial play with sub products and add spesific subproducts to
orderproduct.
But, then, how to list all SubProducts that belongs to spesific order?
Specially thing stops at "Product", it doesn't know which one of those
two subproducts it actually is.
I did following quick hack to get around this problem (added this to
Product model):
def _get_subproduct(self):
for attr in ['subproduct1', 'subproduct2', ]:
try:
return getattr(self, attr)
except ObjectDoesNotExist:
pass
subproduct = property(_get_subproduct)
This just don't feel optimal solution, specially it hits database for
every subitem tried.
Any better way to achieve same without hitting that much database?
--
Jani Tiainen
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---