diff --git a/postgres_composite_types/composite_type.py b/postgres_composite_types/composite_type.py index 49652f7..60bbae2 100644 --- a/postgres_composite_types/composite_type.py +++ b/postgres_composite_types/composite_type.py @@ -61,8 +61,7 @@ def __new__(cls, name, bases, attrs): # create the field for this Type attrs["Field"] = type(f"{name}.Field", (BaseField,), {}) - attrs["__id"] = DummyField(primary_key=True, serialize=False) - attrs["__id"].name = "pk" + attrs[DummyField.name] = DummyField(primary_key=True, serialize=False) # Use an EmptyManager for everything as types cannot be queried. meta_obj.default_manager_name = "objects" diff --git a/postgres_composite_types/fields.py b/postgres_composite_types/fields.py index dbbdbb5..60f0479 100644 --- a/postgres_composite_types/fields.py +++ b/postgres_composite_types/fields.py @@ -16,6 +16,8 @@ class DummyField(Field): all models to have a primary key. """ + name = "_id_not_used" + class BaseField(Field): """Base class for the field that relates to this type.""" @@ -73,7 +75,7 @@ def to_python(self, value): **{ field.name: field.to_python(value.get(field.name)) for field in self._composite_type_model._meta.fields - if field.name != "pk" + if field.name != DummyField.name } ) @@ -89,6 +91,6 @@ def value_to_string(self, obj): { field.name: field.value_to_string(value) for field in self._composite_type_model._meta.fields - if field.name != "pk" + if field.name != DummyField.name } ) diff --git a/postgres_composite_types/forms.py b/postgres_composite_types/forms.py index 6629b8e..d4ae85c 100644 --- a/postgres_composite_types/forms.py +++ b/postgres_composite_types/forms.py @@ -12,6 +12,7 @@ from django.utils.translation import gettext as _ from . import CompositeType +from .fields import DummyField LOGGER = logging.getLogger(__name__) @@ -64,7 +65,7 @@ def __init__(self, *args, fields=None, model=None, **kwargs): fields = { field.name: field.formfield() for field in fields or model._meta.fields - if field.name != "pk" + if field.name != DummyField.name } widget = CompositeTypeWidget( diff --git a/postgres_composite_types/operations.py b/postgres_composite_types/operations.py index b226097..a73309a 100644 --- a/postgres_composite_types/operations.py +++ b/postgres_composite_types/operations.py @@ -16,7 +16,7 @@ def sql_create_type(type_name, fields, schema_editor): fields_list = ", ".join( sql_field_definition(field_name, field, schema_editor) for field_name, field in fields - if field_name != "__id" + if field_name != DummyField.name ) quoted_name = schema_editor.quote_name(type_name) return f"CREATE TYPE {quoted_name} AS ({fields_list})" @@ -33,7 +33,10 @@ class CreateType(CreateModel): reversible = True def __init__(self, *, name: str, fields, options) -> None: - fields = [("__id", DummyField(primary_key=True, serialize=False)), *fields] + fields = [ + (DummyField.name, DummyField(primary_key=True, serialize=False)), + *fields, + ] super().__init__(name, fields, options) def describe(self): diff --git a/postgres_composite_types/quoting.py b/postgres_composite_types/quoting.py index 23865fe..8571564 100644 --- a/postgres_composite_types/quoting.py +++ b/postgres_composite_types/quoting.py @@ -1,5 +1,7 @@ from psycopg2.extensions import ISQLQuote, adapt +from .fields import DummyField + __all__ = ["QuotedCompositeType"] @@ -23,7 +25,7 @@ def __init__(self, obj): self.model.registered_connection, ) for field in self.model._meta.fields - if field.name != "pk" + if field.name != DummyField.name ) )