I have a model with a JSON field or a link to a CouchDB document. I can currently access the dynamic information in a way such as:
genericdocument.objects.get(pk=1) == genericdocument.json_field['sample subfield']
instead I would like to use
genericdocument.sample_subfield
to maintain compatibility with all the apps the project currently shares.
UPDATE
This is very close to what I'm trying to achieve: http://pypi.python.org/pypi/eav-django/1.0.0 but using Django standard fields instead.
This question helped me find the solution: How do I override __getattr__ in Python without breaking the default behavior?
The dictionary returned by get_couchdb_data() is exposed as attributes of the model instance.
does this help:
That should work ok for known "sample_subfields" if there are not too many.
If you want to be able to call
genericdocument.XXX
(where XXX can be anything), then you would have to overwrite__getattribute__
in your model which I would not really put into consideratino since afaik Django does that itself.