Per instance dynamic fields django model

2019-06-08 06:57发布

问题:

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.

回答1:

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.



回答2:

This question helped me find the solution: 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.