Don't know if this is the solution for you, but you could take a look
here: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#id1

and do something like this:

class Product(models.Model):
        name = models.CharField(max_length=64)
        
        class Meta:
                abstract = True

class SubProduct1(Product):
        prop1 = models.FloatField()

class SubProduct2(Product):
        prop1 = models.EmailField()

class OrderProduct(models.Model):
        order = models.ForeignKey(Order)
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        product = generic.GenericForeignKey()

Good luck!


On Tue, 2009-09-22 at 18:56 +0300, Jani Tiainen wrote:
> 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?
> 


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to