Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement EAV in Django

I need to implement a fairly standard entity-attribute-value hierarchy. There are devices of multiple types, each type has a bunch of settings it can have, each individual device has a set of particular values for each setting. It seems that both django-eav and eav-django packages are no longer maintained, so I guess I need to roll my own. But how do I architect this? So far, I am thinking something like this (skipping a lot of detail)

class DeviceType(Model):
    name = CharField()

class Device(Model):
    name = CharField()
    type = ForeignKey(DeviceType)

class Setting(Model):
    name = CharField()
    type = CharField(choices=(('Number', 'int'), ('String', 'str'), ('Boolean', 'bool')))
    device_type = ForeignKey(DeviceType)

class Value(Model):
    device = ForeignKey(Device)
    setting = ForeignKey(Setting)
    value = CharField()

    def __setattr__(self, name, value):
        if name == 'value':
            ... do validation based on the setting type ...

    def __getattr__(self, name):
        if name == 'value':
            ... convert string to whatever is the correct value for the type ...

Am I missing something? Is there a better way of doing this? Will this work?

like image 977
Mad Wombat Avatar asked Apr 29 '16 15:04

Mad Wombat


1 Answers

The question went unanswered for a long time and in the meantime things have changed. In particular, a fork was made of django-eav. It is Django EAV 2

Perhaps the need to use the EAV model does not happen very often, so finding this storage library is not immediate.

I apologize for not giving an in-depth answer to this question, but I have just started using this library too, so I prefer to avoid giving inaccurate directions.

However, I believe this link can save time for anyone who needs it.

like image 199
cisba Avatar answered Oct 21 '22 13:10

cisba