Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django end-user defined fields, how to? [duplicate]

Possible Duplicate:
Django dynamic model fields

Good Morning guys! Scenario is the following. For some models on Django, I would like to allow the end user to define his own fields. It would be great if I could keep all Django awesome features like the ORM, so I can still do calls like field__gte to search on the model, still have field validation according to field type, etc. I've thought about two ways of doing this, and I'm more than open for new suggestions. Any feedback would be VERY appreciated.

  1. The first approach, is the Entity-Attribute-Value ( http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model ), which django already has an app for. See http://code.google.com/p/django-custom-field/ I think this would be an OK solution, but I lose the ability to do "mymodel.objects.filter(custom_field_x=something)". Maybe there's a way to regain the ORM, any ideas? But I've heard so many bad stories about this method that I'm little scared to use it.

  2. The second approach would be to have a database table for each of the users (probably no more than a 1000). I've read django has something in the lines of inspectdb, which actually checks which fields are there and produces the model for you. This could be useful but I think maybe I should store the fields this particular user has created and somehow dinamically tell django, hey, we also have this fields in this model. Is this possible? I know it's generally bad to have different tables for each user, but considering this scenario, how would you guys rate this method, would it be ok to have one table for each user?

The model that requires custom fields is for example Person. They might want a custom field to store address, blood type, or any other thing.

MANY THANKS in advance! Have a nice sunday!

Very similar: How to create user defined fields in Django -- but only talks about the EAV, which I would like to avoid. I'm open for new ideas!

like image 233
Clash Avatar asked Nov 05 '22 06:11

Clash


1 Answers

One approach is to use a NoSQL document-based solution such as MongoDB which allows you to store objects that have a fluid structure (no such restrictions as pre-defined columns).

Pros:

  1. No restriction on custom field types, number of types of fields, etc.
  2. Retains ORM functionality (django-mongodb)
  3. Other various benefits of NoSQL - which you can read about online
  4. Avoids EAV

Cons:

  1. Need to setup NoSQL server
  2. Additional knowledge required on NoSQL concepts (documents vs. tables)
  3. You may have to maintain two databases - if you decide not to migrate your entire solution to NoSQL (multi-db)

EDIT:

After reading the comments its worth pointing out that depending on which NoSQL solution you go with, you may not need reversion support. CouchDB, for example has built in support for document versioning.

like image 155
Burhan Khalid Avatar answered Nov 14 '22 23:11

Burhan Khalid