Skip to content

Commit 589516c

Browse files
committed
Remove unique_for_date/month/year on model field
1 parent f0096d9 commit 589516c

File tree

2 files changed

+4
-76
lines changed

2 files changed

+4
-76
lines changed

plain-models/plain/models/base.py

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,15 +1145,9 @@ def validate_unique(self, exclude=None):
11451145
Check unique constraints on the model and raise ValidationError if any
11461146
failed.
11471147
"""
1148-
unique_checks, date_checks = self._get_unique_checks(exclude=exclude)
1148+
unique_checks = self._get_unique_checks(exclude=exclude)
11491149

1150-
errors = self._perform_unique_checks(unique_checks)
1151-
date_errors = self._perform_date_checks(date_checks)
1152-
1153-
for k, v in date_errors.items():
1154-
errors.setdefault(k, []).extend(v)
1155-
1156-
if errors:
1150+
if errors := self._perform_unique_checks(unique_checks):
11571151
raise ValidationError(errors)
11581152

11591153
def _get_unique_checks(self, exclude=None, include_meta_constraints=False):
@@ -1194,9 +1188,6 @@ def _get_unique_checks(self, exclude=None, include_meta_constraints=False):
11941188
if not any(name in exclude for name in constraint.fields):
11951189
unique_checks.append((model_class, constraint.fields))
11961190

1197-
# These are checks for the unique_for_<date/year/month>.
1198-
date_checks = []
1199-
12001191
# Gather a list of checks for fields declared as unique and add them to
12011192
# the list of checks.
12021193

@@ -1211,13 +1202,8 @@ def _get_unique_checks(self, exclude=None, include_meta_constraints=False):
12111202
continue
12121203
if f.unique:
12131204
unique_checks.append((model_class, (name,)))
1214-
if f.unique_for_date and f.unique_for_date not in exclude:
1215-
date_checks.append((model_class, "date", name, f.unique_for_date))
1216-
if f.unique_for_year and f.unique_for_year not in exclude:
1217-
date_checks.append((model_class, "year", name, f.unique_for_year))
1218-
if f.unique_for_month and f.unique_for_month not in exclude:
1219-
date_checks.append((model_class, "month", name, f.unique_for_month))
1220-
return unique_checks, date_checks
1205+
1206+
return unique_checks
12211207

12221208
def _perform_unique_checks(self, unique_checks):
12231209
errors = {}
@@ -1268,54 +1254,6 @@ def _perform_unique_checks(self, unique_checks):
12681254

12691255
return errors
12701256

1271-
def _perform_date_checks(self, date_checks):
1272-
errors = {}
1273-
for model_class, lookup_type, field, unique_for in date_checks:
1274-
lookup_kwargs = {}
1275-
# there's a ticket to add a date lookup, we can remove this special
1276-
# case if that makes it's way in
1277-
date = getattr(self, unique_for)
1278-
if date is None:
1279-
continue
1280-
if lookup_type == "date":
1281-
lookup_kwargs["%s__day" % unique_for] = date.day
1282-
lookup_kwargs["%s__month" % unique_for] = date.month
1283-
lookup_kwargs["%s__year" % unique_for] = date.year
1284-
else:
1285-
lookup_kwargs[f"{unique_for}__{lookup_type}"] = getattr(
1286-
date, lookup_type
1287-
)
1288-
lookup_kwargs[field] = getattr(self, field)
1289-
1290-
qs = model_class._default_manager.filter(**lookup_kwargs)
1291-
# Exclude the current object from the query if we are editing an
1292-
# instance (as opposed to creating a new one)
1293-
if not self._state.adding and self.pk is not None:
1294-
qs = qs.exclude(pk=self.pk)
1295-
1296-
if qs.exists():
1297-
errors.setdefault(field, []).append(
1298-
self.date_error_message(lookup_type, field, unique_for)
1299-
)
1300-
return errors
1301-
1302-
def date_error_message(self, lookup_type, field_name, unique_for):
1303-
opts = self._meta
1304-
field = opts.get_field(field_name)
1305-
return ValidationError(
1306-
message=field.error_messages["unique_for_date"],
1307-
code="unique_for_date",
1308-
params={
1309-
"model": self,
1310-
"model_name": opts.model_name,
1311-
"lookup_type": lookup_type,
1312-
"field": field_name,
1313-
"field_label": field.name,
1314-
"date_field": unique_for,
1315-
"date_field_label": opts.get_field(unique_for).name,
1316-
},
1317-
)
1318-
13191257
def unique_error_message(self, model_class, unique_check):
13201258
opts = model_class._meta
13211259

plain-models/plain/models/fields/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ class Field(RegisterLookupMixin):
126126
"null": "This field cannot be null.",
127127
"blank": "This field cannot be blank.",
128128
"unique": "A %(model_name)s with this %(field_label)s already exists.",
129-
"unique_for_date": "%(field_label)s must be unique for %(date_field_label)s %(lookup_type)s.",
130129
}
131130
system_check_deprecated_details = None
132131
system_check_removed_details = None
@@ -177,9 +176,6 @@ def __init__(
177176
default=NOT_PROVIDED,
178177
editable=True,
179178
serialize=True,
180-
unique_for_date=None,
181-
unique_for_month=None,
182-
unique_for_year=None,
183179
choices=None,
184180
db_column=None,
185181
db_tablespace=None,
@@ -197,9 +193,6 @@ def __init__(
197193
self.default = default
198194
self.editable = editable
199195
self.serialize = serialize
200-
self.unique_for_date = unique_for_date
201-
self.unique_for_month = unique_for_month
202-
self.unique_for_year = unique_for_year
203196
if isinstance(choices, ChoicesMeta):
204197
choices = choices.choices
205198
if isinstance(choices, collections.abc.Iterator):
@@ -538,9 +531,6 @@ def deconstruct(self):
538531
"default": NOT_PROVIDED,
539532
"editable": True,
540533
"serialize": True,
541-
"unique_for_date": None,
542-
"unique_for_month": None,
543-
"unique_for_year": None,
544534
"choices": None,
545535
"db_column": None,
546536
"db_comment": None,

0 commit comments

Comments
 (0)