Skip to content

Model unique_for_month changes ability to partially update ModelSerializer #6341

@jambonrose

Description

@jambonrose

If I have a model and a ModelSerializer, then I can partially update model instances using the serializer. For instance, given a Post model, and a basic PostSerializer, shown below, I can update a Post instance using the serializer.

class Post(Model):
    title = CharField(max_length=63)
    slug = SlugField(max_length=63)
    pub_date = DateField(default=date.today)
class PostSerializer(ModelSerializer):
    class Meta:
        model = Post
        fields = "__all__"

I use the code below to achieve the actual update.

post = Post.objects.create(title="first", slug="slug")
s_post = PostSerializer(instance=post, data={"title": "second"}, partial=True)
if s_post.is_valid():
    s_post.save()  # updates the title from first->second

The problem is that if I add a unique_for_month constraint on the SlugField, I lose the ability to use the code above.

class Post(Model):
    title = CharField(max_length=63)
    slug = SlugField(max_length=63, unique_for_month="pub_date")
    pub_date = DateField(default=date.today)

If I run the update code with the PostSerializer as shown above, then the serializer will have the following errors:

{
    'slug': [
        ErrorDetail(
            string='This field is required.',
            code='required'
        ),
    ],
    'pub_date': [
        ErrorDetail(
            string='This field is required.',
            code='required'
        )
    ],
}

I believe this is a bug, but apologize if I've misunderstood.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions