views:

1125

answers:

4

I am working on a Django application which contains an Offer model. An Offer instance contains the pricing conditions and points to a product definition. The product model is actually a hierarchy (I have a Television model, a Camcorder model, etc.). So I would like the Offer model to contain a polymorphic (or "generic") association to point to any product.

For now, all I have found is this to use the generic associations in the ContentTypes application. This might do, but I am looking for alternatives, if any.

Thanks for your help.

(one solution per answer please)

A: 

You might want to have a look at model inheritance.

Guillaume
I obviously agree, but could you please spell out the answer to make it truly useful to someone that would be asking it. That way I can +1 it.
Ivan
Thanks, but I don't think this is the answer. What I need is to have something like: class Offer(models.Model): product = models.ForeignKey(Product) ...Where Product is an abstract class, and the actual referenced object is an instance of a concrete subclass.Any ideas?
MiniQuark
+2  A: 

If you only need to point to "any product," not any model, then the solution is to have a Product model that all products inherit from (i.e. Television and Camcorder are both subclasses of Product), and give your Offer model a ForeignKey to Product.

Carl Meyer
Yes, this is the general idea, but if I do only that, then I have 2 drawbacks: firstly, I cannot use multiple-table inheritance, secondly when I go from an offer to a product, I get a Product instance instead of a Camcorder (or other concrete class) instance.
MiniQuark
You can automatically transform a Product instance into an (e.g.) Camcorder instance if you store an FK to the "child" ContentType (Camcorder) at creation time. More discussion at http://groups.google.com/group/django-users/browse_thread/thread/f4241bc16455f92d/7268c3f7bca6b046?lnk=raot
Carl Meyer
What do you mean by "I cannot use multiple-table inheritance"? Multiple-table inheritance is how Django implements inheritance from non-abstract base classes (unlike Rails, which does single-table inheritance). Your choices here are inheritance or generic relations; there's no other silver bullet.
Carl Meyer
A: 

I am having the same problem but django.db does not seem to directly support this. My problem is even a bit worse because my model have many branching inheritance levels with relationships among them.

Perhaps the base classes, at least the lowest one, will need to have a type tag similar to activerecord (RoR) implementation so, when the object is fetched it can be correctly typed.

I have not yet browsed django.db sources but I think it is not a simple change... I will give it a try, anyway.

Flávio de Sousa
+1  A: 

ContentTypes is the right approach. This is because the ForignKey can only point to one type of table so you need to pass through the an intermediate table and do your split depending on the different type.

So model inheritance for the class hierarchy, but ContentType for the foreign keys.

ivan