Skip to content

List Serializer Breaks on Unique Constraint Validator #9484

Open
@max-muoto

Description

@max-muoto

Checklist

3.15 added support for unique constraint validators in serializers: #7438

This unfortunately breaks when it comes to list serializers. It could make sense these not to run for list serializers if it's difficult to do so in an efficient manner without an O(N) query, and document that fact.

Reproducible Example

from django.db import models
from django.db.models import UniqueConstraint, Q
from rest_framework import serializers

class Pet(models.Model):
    name = models.CharField(max_length=100)
    animal_type = models.CharField(max_length=100)
    can_fly = models.BooleanField(null=True)

    class Meta:
        constraints = [
            UniqueConstraint(
                fields=["name", "animal_type"],
                name="unique_pet",
            )
        ]

class PetSerializer(serializers.ModelSerializer):
    class Meta:
        model = Pet
        fields = ('name', 'animal_type', 'can_fly')

class PetListSerializer(serializers.ListSerializer):
    child = PetSerializer()

    def create(self, validated_data):
        pets = [Pet(**item) for item in validated_data]
        return Pet.objects.bulk_create(pets)

instances = Pet.objects.all()
new_data = [
    {"name": "Polly", "animal_type": "Parrot", "can_fly": True},
    {"name": "Penguin", "animal_type": "Bird", "can_fly": False},
]
serializer = PetListSerializer(instances, data=new_data, many=True)
if serializer.is_valid(raise_exception=True):
    serializer.save()  # This will create all pets in a single query

Running this will yield

AttributeError: 'BaseQuerySet' object has no attribute 'pk'

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