@@ -218,15 +218,25 @@ class ObjectTagUpdateBodySerializer(serializers.Serializer): # pylint: disable=
218218 tagsData = serializers .ListField (child = ObjectTagUpdateByTaxonomySerializer (), required = True )
219219
220220
221- def validate_tag_value (value , context ):
221+ def validate_tag_value (value , context , original_value = None ):
222222 """
223- Validate this tag value is unique within the current taxonomy context and
224- does not contain forbidden characters.
223+ Validates the incoming request early:
224+ - This tag is unique, not a duplicate. (The model only validates this if you call `full_clean()`.)
225+ - There are no forbidden / reserved characters present. There is an additional
226+ model-side validation for this as well, but we are keeping this so we can validate
227+ the incoming request immediately.
225228 """
226229 taxonomy_id = context .get ("taxonomy_id" )
230+ original_tag = Tag .objects .filter (taxonomy_id = taxonomy_id , value = original_value ).first () if original_value else None
231+ tag_id = original_tag .pk if original_tag else None
227232 if taxonomy_id is not None :
228- # Check if tag value already exists within this taxonomy. If so, raise a validation error.
229233 queryset = Tag .objects .filter (taxonomy_id = taxonomy_id , value = value )
234+
235+ # Don't compare tag against itself when validating its updated value.
236+ if tag_id :
237+ queryset = queryset .exclude (pk = tag_id )
238+
239+ # Check if tag value already exists within this taxonomy. If so, raise a validation error.
230240 if queryset .exists ():
231241 raise serializers .ValidationError (
232242 f'Tag value "{ value } " already exists in this taxonomy.' , code = 'unique'
@@ -350,7 +360,7 @@ def validate_updated_tag_value(self, value):
350360 """
351361 Run validations for the updated tag value.
352362 """
353- return validate_tag_value (value , self .context )
363+ return validate_tag_value (value , self .context , original_value = self . initial_data . get ( "tag" ) )
354364
355365
356366class TaxonomyTagDeleteBodySerializer (serializers .Serializer ): # pylint: disable=abstract-method
0 commit comments