On 2 Lut, 16:06, Simon Davies <simon...@gmail.com> wrote: > Hi > > I am writing a web shop. I have a shopping cart class, which has a > many to many relationship to an item class, the item class has three > derived classes. The basic schema is shown below: > > class Item(models.Model): > title = models.CharField(max_length=100) > description = models.CharField(max_length=500) > price = models.DecimalField(max_digits=7, decimal_places=2, > verbose_name=u'Price (£)') > > class Bike(Item): > manufacturer = models.CharField(max_length=50) > > class BikePart(Item): > partType = models.CharField(max_length=50, choices=PARTTYPE_CHOICES) > > class Accessory(Item): > accessoryType = models.CharField(max_length=50, > choices=ACCESSORYTYPE_CHOICES) > > class Cart(models.Model): > items = models.ManyToManyField(Item) > > The cart has a manytomany relationship with the item superclass. How > is it possible to get the derived class back though from the > superclass that is stored in the Cart.items field, or is there a > better way of doing this. Will I have to resort to queries through > all of the derived classes checking for the id's that match those of > the objects in Cart.items so something like this: > > for elem in cart.items.all(): > b = UsedBike.objects.all().filter(id=elem.id) > > or is there a neater way of doing this. >
This is a `standard` problem with relational databases, there are several solutions you can find on google, but neither of them is perfect, IMO. You can also take a look at this: http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/ - maybe it can solve your problem. -- Tomasz Zielinski http://pyconsultant.eu -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@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.