[Fixed]-How to store arbitrary name/value key pairs in a Django model?

26👍

Consider representing all custom properties with serialized dict. I used this in a recent project and it worked really well.

 class Widget(models.Model):
      owner = models.ForeignKey(auth.User)
      props = models.TextField(blank=True) # serialized custom data

      @property
      def props_dict(self):
          return simplejson.loads(self.props)

 class UserProfile(models.Model)
      user = models.ForeignKey(auth.User)
      widget_fields = models.TextField(blank=True) # serialized schema declaration

9👍

It looks like you’ve reinvented the triple store. I think it’s a common thing, as we follow the idea of database flexibility to its natural conclusion. Triple stores tend to be fairly inefficient in relational database systems, but there are systems designed specifically for them.

http://en.wikipedia.org/wiki/Triplestore

At the scales you’re talking about, your performance is likely to be acceptable, but they don’t generally scale well without a specialized DB.

3👍

In my opinion, the best way to achieve this sort of completely extensible model is really with EAV (Entity, Attribute, Value). Its basically a way to bring a schemaless non-relational database to SQL. You can read a bunch more about it on wikipedia, http://en.wikipedia.org/wiki/Entity-attribute-value_model but one of the better implementation of it in django is from the EveryBlock codebase. Hope it’s a help!

http://github.com/brosner/everyblock_code/blob/master/ebpub/ebpub/db/models.py

0👍

http://github.com/tuttle/django-expando may be of interest to you.

0👍

When I had an object that could be completely customized by users, I created a field on the model that would contain some JSON in the column. Then you can just serialize back and forth when you need to use it or save it.

However, it does make it harder to use the data in SQL queries.

Leave a comment