-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set charfield as default pk to avoid errors
- Loading branch information
Showing
16 changed files
with
293 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
60 changes: 0 additions & 60 deletions
60
eav/migrations/0010_alter_attribute_datatype_alter_attribute_id_and_more.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Generated by Django 4.2.6 on 2023-12-04 08:31 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
"""Migration to set CharField as default primary key for all models.""" | ||
|
||
dependencies = [ | ||
('eav', '0009_enchance_naming'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='attribute', | ||
name='id', | ||
field=models.CharField( | ||
editable=False, max_length=255, primary_key=True, serialize=False | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name='enumgroup', | ||
name='id', | ||
field=models.CharField( | ||
editable=False, max_length=255, primary_key=True, serialize=False | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name='enumvalue', | ||
name='id', | ||
field=models.CharField( | ||
editable=False, max_length=255, primary_key=True, serialize=False | ||
), | ||
), | ||
migrations.AlterField( | ||
model_name='value', | ||
name='id', | ||
field=models.CharField( | ||
editable=False, max_length=255, primary_key=True, serialize=False | ||
), | ||
), | ||
] |
Oops, something went wrong.