Skip to content

Commit 21cf85f

Browse files
committed
Split non_db_attrs across the fields that introduce them
Field.non_db_attrs used to list every non-db kwarg from every subclass (choices, limit_choices_to, on_delete, related_query_name). Each class now declares only its own kwargs and extends its parent's tuple, so the base no longer hardcodes knowledge of subclass parameters.
1 parent 6dc6c62 commit 21cf85f

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

plain-postgres/plain/postgres/fields/base.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,16 +137,12 @@ class Field[T](RegisterLookupMixin):
137137
"unique": "A %(model_name)s with this %(field_label)s already exists.",
138138
}
139139

140-
# Attributes that don't affect a column definition.
141-
# These attributes are ignored when altering the field.
142-
non_db_attrs = (
140+
# Kwargs that don't affect the column definition; the schema editor
141+
# ignores these when deciding whether an ALTER is needed. Subclasses
142+
# that introduce additional non-db kwargs extend this tuple.
143+
non_db_attrs: tuple[str, ...] = (
143144
"required",
144-
"choices",
145145
"error_messages",
146-
"limit_choices_to",
147-
# Database-level options are not supported, see #21961.
148-
"on_delete",
149-
"related_query_name",
150146
"validators",
151147
)
152148

@@ -733,6 +729,8 @@ def value_from_object(self, obj: Model) -> T | None:
733729
class ChoicesField[T](Field[T]):
734730
"""Base for fields that accept a ``choices=`` parameter."""
735731

732+
non_db_attrs = (*Field.non_db_attrs, "choices")
733+
736734
default_error_messages = {
737735
"invalid_choice": "Value %(value)r is not a valid choice.",
738736
}

plain-postgres/plain/postgres/fields/related.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ def lazy_related_operation(
9696
class RelatedField(FieldCacheMixin, Field):
9797
"""Base class that all relational fields inherit from."""
9898

99+
non_db_attrs = (*Field.non_db_attrs, "limit_choices_to", "related_query_name")
100+
99101
# RelatedField always has a remote_field (never None)
100102
remote_field: ForeignObjectRel
101103
# path_infos is implemented as @cached_property in subclasses (ForeignKey, ManyToManyField)
@@ -367,6 +369,8 @@ class ForeignKeyField(RelatedField):
367369
ForeignKeyField targets the primary key (id) of the remote model.
368370
"""
369371

372+
non_db_attrs = (*RelatedField.non_db_attrs, "on_delete")
373+
370374
empty_strings_allowed = False
371375
default_error_messages = {
372376
"invalid": "%(model)s instance with %(field)s %(value)r does not exist."

0 commit comments

Comments
 (0)