Question

what i want, is to receive advices to define a re-usefull Product model, for a shopping site app, nowadays I know that the store is going to commerce with "clothing", so the product model will have a "season or collections" relationship, but in the future I should use that app to commerce with X product, e.g: "cars" which have "mechanical specifications" relationships.

So Im thinking in metamodels, creating a generic model defined by key/values, but, how to make the relationships?.

But, you are the experts community and I hope you help me to see beyond.

Was it helpful?

Solution

One way to define relationships between one object and many other types of objects is to use a GenericForeignKey and the ContentType framework. I'd guess you would be looking for a Product with some more specific related object such as Jacket. It may look something like this:

class Product(models.Model):
    price = models.FloatField()
    description = models.TextField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    details = generic.GenericForeignKey('content_type', 'object_id')

class Jacket(models.Model):
    size = models.PositiveIntegerField()
    color = models.CharField(max_length=25)

This allows you to define any number of models similar to Jacket which can be associated with your product.

jacket = Jacket(size=69, color="pink")
jacket.save()
prod = Product(price=0.99)
prod.details = jacket  # It's like magic!
prod.save()

This does create the additional work of creating a ton of Models, but it also allows you to be very creative with what data you store and how you store it.

OTHER TIPS

One, popular, option would be to use something like tags. So you'd have the stuff that's common to all items, like an item ID, name, description, price. Then you'd have some more generic tags or described tags in another table, associated with those items. You could have a tag that represents the season, or automobile specifications, etc...

Of course, it really depends on how you design the system to cope with that additional information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top