Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Django EAV or Entity-Attribute-Value is a design pattern that allows flexibility in adding new attributes to an entity without modifying the database schema. In Django, this can be implemented through the django-eav package.

To utilize Django EAV within the models, follow these steps:

  1. Install the django-eav package

    pip install django-eav

  2. Add 'eav' to the INSTALLED_APPS list in settings.py

    INSTALLED_APPS = [ ..., 'eav', ]

  3. Create a new model that extends EAVEntity

    from eav.models import EAVEntity

    class MyEntity(EAVEntity): pass

  4. Define the attributes that can be added to the entity using EAVAttribute

    from eav.models import EAVAttribute

    class MyAttribute(EAVAttribute): pass

  5. Add the attributes to the entity using the addeavvalue method

    entity = MyEntity.objects.create()

    attr = MyAttribute.objects.create(name='my_attribute')

    entity.addeavvalue(attr, 'my_value')

  6. Get the attributes of an entity using the eav attribute

    entity.eav.my_attribute

This way, you can utilize Django EAV within the models to add flexible attributes to entities without modifying the database schema.