@@ -1145,15 +1145,9 @@ def validate_unique(self, exclude=None):
1145
1145
Check unique constraints on the model and raise ValidationError if any
1146
1146
failed.
1147
1147
"""
1148
- unique_checks , date_checks = self ._get_unique_checks (exclude = exclude )
1148
+ unique_checks = self ._get_unique_checks (exclude = exclude )
1149
1149
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 ):
1157
1151
raise ValidationError (errors )
1158
1152
1159
1153
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):
1194
1188
if not any (name in exclude for name in constraint .fields ):
1195
1189
unique_checks .append ((model_class , constraint .fields ))
1196
1190
1197
- # These are checks for the unique_for_<date/year/month>.
1198
- date_checks = []
1199
-
1200
1191
# Gather a list of checks for fields declared as unique and add them to
1201
1192
# the list of checks.
1202
1193
@@ -1211,13 +1202,8 @@ def _get_unique_checks(self, exclude=None, include_meta_constraints=False):
1211
1202
continue
1212
1203
if f .unique :
1213
1204
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
1221
1207
1222
1208
def _perform_unique_checks (self , unique_checks ):
1223
1209
errors = {}
@@ -1268,54 +1254,6 @@ def _perform_unique_checks(self, unique_checks):
1268
1254
1269
1255
return errors
1270
1256
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
-
1319
1257
def unique_error_message (self , model_class , unique_check ):
1320
1258
opts = model_class ._meta
1321
1259
0 commit comments