How to store arbitrary name/value key pairs in a Django model?

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

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 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.

As long as you don't need to sort or filter based on the value of custom attributes, this solution will probably involve less headache (and better performance) than WidgetField and WidgetValue models. It could be cleaned up some by making props a custom field type that automatically serialized and deserializes itself on load/save. – Carl Meyer Oct 29 '09 at 6:25.

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 it's natural conclusion. Triple stores tend to be fairly inefficient in relational database systems, but there are systems designed specifically for them.

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.

1 Agreed. You should probably consider something like Redland library or rdflib (code.google.com/p/rdflib/wiki/IntroStore) instead of django's db layer. The real benefit of this will be in using specialised query languages, like SPARQL.

– Lee B Nov 9 '09 at 7:03.

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, en.wikipedia.org/wiki/Entity-attribute-v... but one of the better implementation of it in django is from the EveryBlock codebase.

Hope it's a help! github.com/brosner/everyblock%5Fcode/blo....

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions