【发布时间】:2015-06-14 20:17:40
【问题描述】:
我是 DRF 和 python 的新手,所以请放轻松...我可以成功获取 RoadSegment 对象,但我不知道如何更新现有对象
我有以下型号:
class RoadSegment(models.Model):
location = models.CharField(max_length=100)
entryline = models.CharField(unique=True, max_length=100)
trafficstate = models.CharField(max_length=100)
使用以下序列化程序:
class RoadSegmentSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = RoadSegment
fields = ('entryline','trafficstate','location')
还有下面的观点
class RoadSegmentViewSet(viewsets.ModelViewSet):
queryset = RoadSegment.objects.all()
serializer_class = serializers.RoadSegmentSerializer
我的 urls.py 如下所示:
router.register(r'roadsegment', RoadSegmentViewSet, base_name='roadsegment-detail')
GET http://127.0.0.1:8000/api/v1/roadsegment/ 返回
[{"entryline":"2nd","trafficstate":"main","location":"downtown"},{"entryline":"3nd","trafficstate":"low","location":"downtown"}]
我希望能够更新现有对象
补丁http://127.0.0.1:8000/api/v1/roadsegment/
{"entryline":"2nd","trafficstate":"SOMENEWVALUE","location":"downtown"}
【问题讨论】:
-
我没有看到您的物品是如何识别的。通常 ID 指向资源 URL,即
/api/v1/roadsegment/42/。 -
你看官方教程了吗?这是一个与您的情况非常相似的示例:django-rest-framework.org/tutorial/3-class-based-views
标签: django django-rest-framework