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.
If I have a model and a
ModelSerializer, then I can partially update model instances using the serializer. For instance, given aPostmodel, and a basicPostSerializer, shown below, I can update aPostinstance using the serializer.I use the code below to achieve the actual update.
The problem is that if I add a
unique_for_monthconstraint on theSlugField, I lose the ability to use the code above.If I run the update code with the
PostSerializeras 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.