django-eav2/README.md

125 lines
3.5 KiB
Markdown
Raw Normal View History

2021-10-21 14:43:15 +00:00
[![Build Status](https://github.com/jazzband/django-eav2/actions/workflows/test.yml/badge.svg)](https://github.com/jazzband/django-eav2/actions/workflows/test.yml)
2021-10-20 15:03:18 +00:00
[![codecov](https://codecov.io/gh/jazzband/django-eav2/branch/master/graph/badge.svg?token=BJk3zS22BS)](https://codecov.io/gh/jazzband/django-eav2)
2021-10-21 18:37:45 +00:00
[![Python Version](https://img.shields.io/pypi/pyversions/django-eav2.svg)](https://pypi.org/project/django-eav2/)
[![Django Version](https://img.shields.io/pypi/djversions/django-eav2.svg?color=green)](https://pypi.org/project/django-eav2/)
2021-06-05 17:56:38 +00:00
[![Jazzband](https://jazzband.co/static/img/badge.svg)](https://jazzband.co/)
2018-06-01 14:15:07 +00:00
2023-10-05 14:31:16 +00:00
## Django EAV 2 - Entity-Attribute-Value storage for Django
2010-09-28 13:39:57 +00:00
2023-10-05 14:31:16 +00:00
Django EAV 2 is a fork of django-eav. You can find documentation <a href="https://django-eav2.rtfd.io">here</a> for more information.
2010-09-28 13:39:57 +00:00
2023-10-05 14:31:16 +00:00
## What is new here ?
2023-10-05 14:31:16 +00:00
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.
2018-06-29 12:33:06 +00:00
## Installation
Install with pip
2021-07-05 14:18:41 +00:00
2018-06-29 12:33:06 +00:00
```bash
pip install django-eav2
```
## Configuration
2022-05-20 19:12:08 +00:00
Add `eav` to `INSTALLED_APPS` in your settings.
```python
INSTALLED_APPS = [
...
'eav',
]
```
2023-10-05 14:31:16 +00:00
Add `django.db.models.UUIDField` or `django.db.models.BigAutoField` as value of `PRIMARY_KEY_FIELD` in your settings
``` python
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.
```python
PRIMARY_KEY_FIELD = "django.db.models.CharField"
```
Run the migrations
```bash
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.
```python
PRIMARY_KEY_FIELD = "django.db.models.BigAutoField" # as exemple
```
Run again the migrations.
```bash
python manage.py makemigrations
python manage.py migrate
```
### Note: Django 2.2 Users
2018-06-01 12:29:17 +00:00
Since `models.JSONField()` isn't supported in Django 2.2, we use [django-jsonfield-backport](https://github.com/laymonage/django-jsonfield-backport) to provide [JSONField](https://docs.djangoproject.com/en/dev/releases/3.1/#jsonfield-for-all-supported-database-backends) functionality.
2018-06-06 23:02:04 +00:00
This requires adding `django_jsonfield_backport` to your `INSTALLED_APPS` as well.
2018-06-06 23:02:04 +00:00
```python
INSTALLED_APPS = [
...
'eav',
'django_jsonfield_backport',
]
2018-06-06 23:02:04 +00:00
```
2018-06-29 12:33:06 +00:00
## Getting started
2018-06-06 23:02:04 +00:00
2018-06-29 12:33:06 +00:00
**Step 1.** Register a model:
```python
import eav
eav.register(Supplier)
2018-06-06 23:02:04 +00:00
```
2018-06-29 12:33:06 +00:00
or with decorators:
```python
from eav.decorators import register_eav
@register_eav
class Supplier(models.Model):
...
2018-06-06 23:02:04 +00:00
```
2018-06-29 12:33:06 +00:00
**Step 2.** Create an attribute:
```python
Attribute.objects.create(name='City', datatype=Attribute.TYPE_TEXT)
2018-06-06 23:02:04 +00:00
```
2018-06-29 12:33:06 +00:00
**Step 3.** Thats it! Youre ready to go:
```python
supplier.eav.city = 'London'
supplier.save()
Supplier.objects.filter(eav__city='London')
# = <EavQuerySet [<Supplier: Supplier object (1)>]>
2018-06-06 23:02:04 +00:00
```
2018-06-29 12:33:06 +00:00
2023-10-05 14:31:16 +00:00
**For futher information? Check out the django eav2 <a href="https://django-eav2.readthedocs.io/en/latest/#documentation">documentation</a>.**
---