3.5 KiB
Django EAV 2 - Entity-Attribute-Value storage for Django
Django EAV 2 is a fork of django-eav. You can find documentation here for more information.
What is new here ?
With this version of django eav, you can use an IntegerField or a UUIDField as the primary key for your eav models. You can also use the natural key for serialization instead of the primary key.
Installation
Install with pip
pip install django-eav2
Configuration
Add eav to INSTALLED_APPS in your settings.
INSTALLED_APPS = [
...
'eav',
]
Add django.db.models.UUIDField or django.db.models.BigAutoField as value of PRIMARY_KEY_FIELD in your settings
PRIMARY_KEY_FIELD = "django.db.models.UUIDField" # as exemple
Note: Primary key mandatory modification field
If the primary key of eav models are to be modified (UUIDField -> BigAutoField, BigAutoField -> UUIDField) in the middle of the project when the migrations are already done, you have to change the value of PRIMARY_KEY_FIELD in your settings.
Step 1
Change the value of PRIMARY_KEY_FIELD into django.db.models.CharField in your settings.
PRIMARY_KEY_FIELD = "django.db.models.CharField"
Run the migrations
python manage.py makemigrations
python manage.py migrate
Step 2
Change the value of PRIMARY_KEY_FIELD into the desired value (django.db.models.BigAutoField or django.db.models.UUIDField) in your settings.
PRIMARY_KEY_FIELD = "django.db.models.BigAutoField" # as exemple
Run again the migrations.
python manage.py makemigrations
python manage.py migrate
Note: Django 2.2 Users
Since models.JSONField() isn't supported in Django 2.2, we use django-jsonfield-backport to provide JSONField functionality.
This requires adding django_jsonfield_backport to your INSTALLED_APPS as well.
INSTALLED_APPS = [
...
'eav',
'django_jsonfield_backport',
]
Getting started
Step 1. Register a model:
import eav
eav.register(Supplier)
or with decorators:
from eav.decorators import register_eav
@register_eav
class Supplier(models.Model):
...
Step 2. Create an attribute:
Attribute.objects.create(name='City', datatype=Attribute.TYPE_TEXT)
Step 3. That’s it! You’re ready to go:
supplier.eav.city = 'London'
supplier.save()
Supplier.objects.filter(eav__city='London')
# = <EavQuerySet [<Supplier: Supplier object (1)>]>
For futher information? Check out the django eav2 documentation.