[Django]-Django – designing models with virtual fields?

13👍

Products have Features.

class Feature( models.Model ):
    feature_name = models.CharField( max_length=128 )
    feature_value = models.TextField()
    part_of = models.ForeignKey( Product )

Like that.

Just a list of features.

p= Product( "iPhone", "Apple", 350 )
p.save()
f= Feature( "mp3 capacity", "16Gb", p )
f.save()

If you want, you can have a master list of feature names in a separate table. Don’t over-analyze features. You can’t do any processing on them. All you do is present them.

👤S.Lott

3👍

Ruby on Rails has a “serialized” field which allows you to pack a dictionary into a text field. Perhaps DJango offers something similar?

This article has an implementation of a SerializedDataField.

2👍

Personally, I’d go with S. Lott’s answer. However, you might want to create a custom JSON Field:

http://svn.navi.cx/misc/trunk/djblets/djblets/util/fields.py

http://www.djangosnippets.org/snippets/377/

👤Tiago

0👍

Go with the inheritance. Create Produce subclasses with their own, additional fields.

Leave a comment