Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using UUIDField as EAV2_PRIMARY_KEY_FIELD value #478

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
413cdf7
add uuid configuration for id generation of eav models
mathiasag7 Sep 6, 2023
8977495
add charfield possibility to eav id models field
mathiasag7 Sep 6, 2023
de7fb79
feat: natural key handler added to models except value model
mathiasag7 Sep 6, 2023
a73e327
feat: natural key handler added to value model
mathiasag7 Sep 6, 2023
6a01f16
Update CHANGELOG.md
mathiasag7 Sep 6, 2023
4be9a24
Update pyproject.toml
mathiasag7 Sep 6, 2023
832d4f3
fix: get attribute for value natural key function
mathiasag7 Sep 7, 2023
ac3c60b
Merge branch 'master' of github.com:mathiasag7/django-eav2-uuid
mathiasag7 Sep 7, 2023
7ebfb85
update package log
mathiasag7 Sep 7, 2023
ce2b606
fix: error on attribute creation
mathiasag7 Sep 7, 2023
dffbe60
Update README.md
mathiasag7 Oct 5, 2023
2c69da6
readme updated
mathiasag7 Oct 5, 2023
e275a7f
readme updated
mathiasag7 Oct 5, 2023
0aa172c
changelog updated
mathiasag7 Oct 6, 2023
33f6279
feat: natural key for serialization and UUIDField possibility for pk …
mathiasag7 Oct 6, 2023
f342f3c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 6, 2023
6e90e9c
test: test_attribute errors corrected on AutoField
mathiasag7 Oct 13, 2023
06e8cd5
Merge branch 'master' of github.com:mathiasag7/django-eav2-uuid
mathiasag7 Oct 13, 2023
13e5e99
Merge branch 'jazzband:master' into master
mathiasag7 Oct 13, 2023
668dcab
test: pytest.ini file added
mathiasag7 Oct 13, 2023
011965f
Merge branch 'master' of github.com:mathiasag7/django-eav2-uuid
mathiasag7 Oct 13, 2023
e64425d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2023
0d705d9
test: pyproject.toml updated"
mathiasag7 Oct 13, 2023
0000e26
migration file created
mathiasag7 Oct 13, 2023
7a00e04
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2023
d26f61a
test updated
mathiasag7 Oct 13, 2023
8bf3fee
Merge branch 'master' of github.com:mathiasag7/django-eav2-uuid
mathiasag7 Oct 13, 2023
4ef7f7a
test updated
mathiasag7 Oct 13, 2023
9407982
revert changes
mathiasag7 Oct 16, 2023
1d70d73
convert tests to pytest
Dresdn Oct 24, 2023
f7cd292
remove test cehcking for ValueError
Dresdn Oct 24, 2023
db893d3
feat: test_natural_keys implemented
mathiasag7 Oct 25, 2023
43365cd
Merge branch 'master' of github.com:mathiasag7/django-eav2-uuid
mathiasag7 Oct 25, 2023
b0c2b50
set charfield as default pk to avoid errors
mathiasag7 Dec 4, 2023
5c0b0d6
changelog updated
mathiasag7 Dec 4, 2023
2f1957f
fix: error in migration files
mathiasag7 Dec 20, 2023
ae01a4c
Merge branch 'jazzband:master' into master
mathiasag7 Mar 8, 2024
1d6f15d
Merge branch 'jazzband:master' into master
mathiasag7 Mar 15, 2024
2ff9292
tests + migration file updated
mathiasag7 Mar 22, 2024
3b8d28c
tests + migration file updated
mathiasag7 Mar 22, 2024
4c6ceed
tests + migration file updated
mathiasag7 Mar 22, 2024
08a2699
tests + migration file updated
mathiasag7 Mar 22, 2024
d35289f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 22, 2024
b2f8124
tests + migration file updated
mathiasag7 Mar 25, 2024
b011b1b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 25, 2024
7fb9e7c
tests + migration file updated
mathiasag7 Mar 29, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
set charfield as default pk to avoid errors
mathiasag7 committed Dec 4, 2023
commit b0c2b508005be55a19023e6ba96e156943a1d2d6
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -14,12 +14,12 @@ We follow [Semantic Versions](https://semver.org/) starting at the `0.14.0` rele

- Fixes querying with multiple eav kwargs [#395](https://github.com/jazzband/django-eav2/issues/395)

## 1.5.0 (2023-09-07)

### Features

- Support for many type of primary key (UUIDField, BigAutoField)
- Support for natural key use for some models for serialization (EnumValue, EnumGroup, Attribute, Value)
- Add support for Django 4.2
- Add support for Python 3.11

## 1.4.0 (2023-07-07)

72 changes: 71 additions & 1 deletion eav/logic/managers.py
Original file line number Diff line number Diff line change
@@ -2,26 +2,96 @@


class EnumValueManager(models.Manager):
"""
Custom manager for `EnumValue` model.

This manager adds utility methods specific to the `EnumValue` model.
"""

def get_by_natural_key(self, value):
"""
Retrieves an EnumValue instance using its `value` as a natural key.

Args:
value (str): The value of the EnumValue instance.

Returns:
EnumValue: The instance matching the provided value.
"""
return self.get(value=value)


class EnumGroupManager(models.Manager):
"""
Custom manager for `EnumGroup` model.

This manager adds utility methods specific to the `EnumGroup` model.
"""

def get_by_natural_key(self, name):
"""
Retrieves an EnumGroup instance using its `name` as a natural key.

Args:
name (str): The name of the EnumGroup instance.

Returns:
EnumGroup: The instance matching the provided name.
"""
return self.get(name=name)


class AttributeManager(models.Manager):
"""
Custom manager for `Attribute` model.

This manager adds utility methods specific to the `Attribute` model.
"""

def get_by_natural_key(self, name, slug):
"""
Retrieves an Attribute instance using its `name` and `slug` as natural keys.

Args:
name (str): The name of the Attribute instance.
slug (str): The slug of the Attribute instance.

Returns:
Attribute: The instance matching the provided name and slug.
"""
return self.get(name=name, slug=slug)


class ValueManager(models.Manager):
"""
Custom manager for `Value` model.

This manager adds utility methods specific to the `Value` model.
"""

def get_by_natural_key(self, attribute, entity_id, entity_uuid):
"""
Retrieve a Value instance using multiple natural keys.

This method utilizes a combination of an `attribute` (defined by its
name and slug), `entity_id`, and `entity_uuid` to retrieve a unique
Value instance.

Args:
attribute (tuple): A tuple containing the name and slug of the
Attribute instance.
entity_id (int): The ID of the associated entity.
entity_uuid (str): The UUID of the associated entity.

Returns:
Value: The instance matching the provided keys.
"""
from eav.models import Attribute

attribute = Attribute.objects.get(name=attribute[0], slug=attribute[1])

return self.get(
attribute=attribute, entity_id=entity_id, entity_uuid=entity_uuid
attribute=attribute,
entity_id=entity_id,
entity_uuid=entity_uuid,
)
54 changes: 40 additions & 14 deletions eav/logic/object_pk.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
import uuid

from functools import partial
from django.db import models
from typing import Type

from django.conf import settings
from django.db import models

#: Constants
_DEFAULT_CHARFIELD_LEN: int = 40

_FIELD_MAPPING = {
"django.db.models.UUIDField": partial(
models.UUIDField,
primary_key=True,
editable=False,
default=uuid.uuid4,
),
"django.db.models.CharField": partial(
models.CharField,
primary_key=True,
editable=False,
max_length=_DEFAULT_CHARFIELD_LEN,
),
}


def get_pk_format() -> Type[models.Field]:
"""
Get the primary key field format based on the Django settings.

This function returns a field factory function that corresponds to the
primary key format specified in Django settings. If the primary key
format is not recognized, it defaults to using BigAutoField.

Returns:
Type[models.Field]: A field factory function that can be used to
create the primary key field instance.
"""
field_factory = _FIELD_MAPPING.get(
settings.EAV2_PRIMARY_KEY_FIELD,
partial(models.BigAutoField, primary_key=True, editable=False),
)

def get_pk_format():
if settings.EAV2_PRIMARY_KEY_FIELD == "django.db.models.UUIDField":
PrimaryField = partial(
models.UUIDField, primary_key=True, editable=False, default=uuid.uuid4
)
elif settings.EAV2_PRIMARY_KEY_FIELD == "django.db.models.CharField":
PrimaryField = partial(
models.CharField, primary_key=True, editable=False, max_length=40
)
else:
PrimaryField = partial(models.BigAutoField, primary_key=True, editable=False)
return PrimaryField()
# Create and return the field instance
return field_factory()

This file was deleted.

28 changes: 11 additions & 17 deletions eav/migrations/0010_dynamic_pk_type_for_models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Generated by Django 4.2.6 on 2023-12-04 08:31

from django.db import migrations, models


class Migration(migrations.Migration):
"""Migration to use BigAutoField as default for all models."""
"""Migration to set CharField as default primary key for all models."""

dependencies = [
('eav', '0009_enchance_naming'),
@@ -12,37 +14,29 @@ class Migration(migrations.Migration):
migrations.AlterField(
model_name='attribute',
name='id',
field=models.BigAutoField(
editable=False,
primary_key=True,
serialize=False,
field=models.CharField(
editable=False, max_length=255, primary_key=True, serialize=False
),
),
migrations.AlterField(
model_name='enumgroup',
name='id',
field=models.BigAutoField(
editable=False,
primary_key=True,
serialize=False,
field=models.CharField(
editable=False, max_length=255, primary_key=True, serialize=False
),
),
migrations.AlterField(
model_name='enumvalue',
name='id',
field=models.BigAutoField(
editable=False,
primary_key=True,
serialize=False,
field=models.CharField(
editable=False, max_length=255, primary_key=True, serialize=False
),
),
migrations.AlterField(
model_name='value',
name='id',
field=models.BigAutoField(
editable=False,
primary_key=True,
serialize=False,
field=models.CharField(
editable=False, max_length=255, primary_key=True, serialize=False
),
),
]
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.