views:

85

answers:

2

I have a model with a JSON field or a link to a CouchDB document. I can currently access the dynamic informaction in a way such as:

genericdocument.objects.get(pk=1) == genericdocument.json_field['sample subfield'] instead I would like 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.

+1  A: 

does this help:

class GenericDocument(models.Model):
...
@property
def sample_subfield(self):
    return self.json_field['sample_subfield']

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.

mawimawi
A: 

This question helped me find the solution: http://stackoverflow.com/questions/2405590/how-do-i-override-getattr-in-python-without-breaking-the-default-behavior

class GenericDocument(models.Model):
    def __getattr__(self, name):
        data = self.get_couchdb_data()
        if name in data.keys():
            return data[name]
        else:
            raise AttributeError

The dictionary returned by get_couchdb_data() is exposed as attributes of the model instance.

Roberto Rosario