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

UniqueTogetherValidator does not work withsource kwarg #9442

Closed
@r-thomson

Description

@r-thomson

My team has run into a bug inUniqueTogetherValidator where it does not work properly with fields with thesource attribute. If a field has asource that is different than the field's name on the serializer, then that field is effectively ignored when checking for uniqueness violations, and theValidationError is not raised.

I am using a serializer set up like this. Note thatUniqueTogetherValidator is expecting the serializer's name for thefields argument— you'll get an erroron this line if you try to provide thesource name instead.

classExampleSerializer(serializers.Serializer):list_id=serializers.PrimaryKeyRelatedField(source='list')position=serializers.IntegerField(source='ordering_key')classMeta:validators= [UniqueTogetherValidator(queryset=ToDoItem.objects.all(),fields=['list_id','position']            )        ]

I believe the bug occurs at this position in the code:

else:
# Ignore validation if all field values are unchanged
checked_values= [
value
forfield,valueinattrs.items()
iffieldinself.fieldsandvalue!=getattr(serializer.instance,field)
]
ifchecked_valuesandNonenotinchecked_valuesandqs_exists(queryset):
field_names=', '.join(self.fields)
message=self.message.format(field_names=field_names)
raiseValidationError(message,code='unique')

Online 172, we check iffield (which comes fromsource) is inself.fields (which is the name on the serializer). If we're looking at the serializer above, thenattrs.keys() would be['list', 'ordering_key'] andself.fields is['list_id', 'position'].

This means thatchecked_values will always be empty, and aValidationError will never be raised.

Checklist

  • Raised initially as discussion #...
  • This is not a feature request suitable for implementation outside this project. Please elaborate what it is:
    • compatibility fix for new Django/Python version ...
    • other type of bug fix
    • other type of improvement that does not touch existing code or change existing behavior (e.g. wrapper for new Django field)
  • I have reduced the issue to the simplest possible case.

REST Framework version: 3.15.1

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