Skip to content

Commit

Permalink
Use a name that doesn't have double underscore for DummyField
Browse files Browse the repository at this point in the history
Double underscore causes errors in lookups
  • Loading branch information
jleclanche committed Dec 14, 2022
1 parent b04b900 commit 4530085
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
3 changes: 1 addition & 2 deletions postgres_composite_types/composite_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions postgres_composite_types/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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
}
)

Expand All @@ -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
}
)
3 changes: 2 additions & 1 deletion postgres_composite_types/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.utils.translation import gettext as _

from . import CompositeType
from .fields import DummyField

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions postgres_composite_types/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})"
Expand All @@ -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):
Expand Down
4 changes: 3 additions & 1 deletion postgres_composite_types/quoting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from psycopg2.extensions import ISQLQuote, adapt

from .fields import DummyField

__all__ = ["QuotedCompositeType"]


Expand All @@ -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
)
)

Expand Down

0 comments on commit 4530085

Please sign in to comment.