Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

SerializerUniqueConstraint validation fails incorrectly on create with conditional fields #9707

Open
@nefrob

Description

@nefrob

Issue:

Adding the following test to TestUniqueConstraintValidation demonstrates an issue in the serializer’s unique constraint validation logic:

deftest_unique_constraint_create(self):classSourceUniqueConstraintSerializer(serializers.ModelSerializer):raceName=serializers.CharField(source="race_name")classMeta:model=UniqueConstraintModelfields= ("raceName","position","global_id","fancy_conditions")UniqueConstraintModel.objects.exclude(pk=self.instance.pk).delete()data= {"race_name":"other","position":1,"global_id":3,"fancy_conditions":1,    }obj=UniqueConstraintModel.objects.create(**data)obj.delete()serializer=SourceUniqueConstraintSerializer(data=data)assertserializer.is_valid()# This unexpectedly fails
  • Creating a UniqueConstraintModel instance directly with UniqueConstraintModel.objects.create() succeeds, indicating no unique constraint violation occurs at the database level.
  • However, validating the same data through the ModelSerializer fails.

Root cause:
The unique validator used by the serializer does not account for theglobal_id field during validation offancy_conditions, even thoughglobal_id is part of the conditional logic in the unique constraint.

As a result, the validation incorrectly identifies a conflict based only onfancy_conditions, without considering that theglobal_id in the input data should exempt it from the constraint.

Expected behavior:

Serializer validation should succeed in this case, consistent with the model-level behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp