1243916142
1243916142

Reputation: 385

How can I write a custom fields serializer only update specified fields?

How can I write a custom fields serializer only update specified fields ?

I have a Model:

class TodoList(models.Model):

    content = models.CharField(max_length=64)
    user = models.ForeignKey(to=User, related_name="todolists")
    is_finish = models.BooleanField(default=False)

    ctime = models.DateTimeField(auto_now_add=True, null=True, blank=True)
    uptime = models.DateTimeField(auto_now=True, null=True, blank=True)

    def __str__(self):
        return self.name
    def __unicode__(self):
        return self.name

I only want to update the is_finish field:

class TodoListUpdateIsFinishSerializer(ModelSerializer):
    class Meta:
        model = TodoList
        fields = ["id", "is_finish"]

In my view:

class TodoListUpdateIsFinishAPIView(RetrieveUpdateAPIView):
    serializer_class = TodoListUpdateIsFinishSerializer
    permission_classes = []
    queryset = TodoList.objects.all()

But when I access this view, there comes issue:

Expected view TodoListUpdateIsFinishAPIView to be called with a URL keyword argument named "pk". Fix your URL conf, or set the .lookup_field attribute on the view correctly.

I tried add lookup_field = "id" or lookup_field = "pk", but still not work.


EDIT My urlpatterns is bellow:

url(r'^todolist/update_isfinish/$', TodoListUpdateIsFinishAPIView.as_view(), name='todolist-update_isfinish')

Upvotes: 0

Views: 82

Answers (1)

Exprator
Exprator

Reputation: 27523

url(r'^todolist/update_isfinish/(?P<pk>\d+)/$', TodoListUpdateIsFinishAPIView.as_view(), name='todolist-update_isfinish')

use this url as django need the url parameter to update

Upvotes: 1

Related Questions