Reputation: 103
I built an api with django and I have a picture model that comprises an imagefield. I'm trying to perform a partial update
I already tried setting partial = True
to the Serializer's definition
the serializer is
class PictureSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Picture
fields = ("id", "name", "picture", "button_1", "button_2", "button_3", "button_4")
partial = True
the model is
class Picture(models.Model):
name = models.CharField(max_length=250)
picture = models.ImageField(upload_to="pictures/", null=True, blank=True)
button_1 = models.BooleanField(default=False)
button_2 = models.BooleanField(default=False)
button_3 = models.BooleanField(default=False)
button_4 = models.BooleanField(default=False)
printed = models.IntegerField(default=0)
the view is
class PictureViewSet(viewsets.ModelViewSet):
queryset = Picture.objects.all()
serializer_class = PictureSerializer
Upvotes: 0
Views: 123
Reputation: 2412
Expose your ViewSet
in urls.py
:
router = routers.DefaultRouter()
router.register(r'pictures', PictureViewSet, base_name='picture')
urlpatterns = [
url(r'', include(router.urls)),
]
Send PATCH
request with JSON data you want to update to the exposed endpoint. Mind that PATCH requests go to instance endpoint of the instance you want to update so your URL will look like this: .../pictures/<picture_id>
.
{
"name": "updated_name"
}
You don't need the partial = True
part.
Also when there are far less excluded fields than included fields in your serializer you can do:
class Meta:
model = Picture
exclude = ("printed", )
Upvotes: 3