2017-10-17 17 views
0

我有以下的模型結構,Django的REST框架扭轉洋場不更新,返回空列表

class Bill(models.Model): 
    created_on = models.DateTimeField(auto_now_add=True) 
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='bill_created_by') 

    total_bill_amount = models.IntegerField(blank=True, null=True) 

class Route(models.Model): 
    start_time = models.DateTimeField(auto_now=False,blank=True, null=True) 
    end_time = models.DateTimeField(auto_now=False,blank=True, null=True) 
    start_lat = models.FloatField(default=0.0,blank=True, null=True) 
    start_lon = models.FloatField(default=0.0,blank=True, null=True) 
    end_lat = models.FloatField(default=0.0,blank=True, null=True) 
    end_lon = models.FloatField(default=0.0,blank=True, null=True) 
    distance = models.FloatField(default=0.0,blank=True, null=True) 
    transport = models.CharField(max_length=300,blank=True, null=True, choices=ROUTE_MODE, default=ROUTE_MODE[0]) 
    fare = models.IntegerField(blank=True, null=True) 
    purpose = models.CharField(max_length=1000, blank=True, null=True) 
    bill = models.ForeignKey(Bill, related_name="routes",on_delete=models.CASCADE, blank=True, null=True) 
    user = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True, null=True, related_name='routes', max_length=255) 

和序列化,

class BillSerializer(ModelSerializer): 
    routes = RouteSerializer(many=True, read_only=True) 
    class Meta: 
     model = Bill 
     fields = ('id','created_on','created_by','total_bill_amount','routes') 

lass RouteSerializer(ModelSerializer): 
    all_directions = AllDirectionSerializer(many=True, read_only=True) 
    user = ReadOnlyField(source='user.email') 
    bill = ReadOnlyField(source='bill.id') 
    class Meta: 
     model = Route 
     fields = ('id','start_time','end_time','start_lat','start_lon','fare', 
     'end_lat','end_lon','distance','transport','all_directions','user', 'bill') 

Bill API使用視圖集中,

class BillViewSet(viewsets.ModelViewSet): 
    queryset = Bill.objects.all() 
    serializer_class = BillSerializer 
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,IsOwnerOrReadOnly,) 

現在如果我想更新Bill根據串行結構API與Routes列表,這樣

**calling put method on Bill api where Bill id is 29** 

    { 
    "id": 29, 
    "created_on": "2017-10-15T10:05:19.786057Z", 
    "created_by": 4, 
    "total_bill_amount": 301, 
    "routes": [ 
    { 
     "id": 31, 
     "start_time": null, 
     "end_time": null, 
     "start_lat": 23.77201, 
     "start_lon": 90.3602333333333, 
     "fare": 0, 
     "end_lat": 0.0, 
     "end_lon": 0.0, 
     "distance": 0.0, 
     "transport": "BUS", 
     "all_directions": [], 
     "user": "[email protected]", 
     "bill": 29 
     }, 
     { 
     "id": 32, 
     "start_time": null, 
     "end_time": null, 
     "start_lat": 23.7715316666667, 
     "start_lon": 90.3604483333333, 
     "fare": 0, 
     "end_lat": 0.0, 
     "end_lon": 0.0, 
     "distance": 0.0, 
     "transport": "BUS", 
     "all_directions": [], 
     "user": "[email protected]", 
     "bill": 29 
     }  
     ] 
     } 

只更新Bill屬性,說我更新total_bill_amount表格301到700,它的成功更新,但routes列表成爲空這樣,

**Result after calling put method** 

{ 
"id": 29, 
"created_on": "2017-10-15T09:47:50.913255Z", 
"created_by": 4, 
"total_bill_amount": 700, 
"routes": [] 
} 

我在這裏幹什麼?爲什麼routes沒有得到更新?

+0

您需要閱讀有關嵌套序列化器的文檔。 – Linovia

+0

你真的可以具體到哪裏是我的代碼問題? @Linovia – RTan

回答

1

默認情況下,DRF不會更新routes。你應該重寫update方法你的串行的,就像這樣:

class BillSerializer(ModelSerializer): 
    routes = RouteSerializer(many=True, read_only=True) 
    class Meta: 
     model = Bill 
     fields =('id','created_on','created_by','total_bill_amount','routes') 

    def update(self, instance, validated_data) 
     instance = super().update(instance, validated_data) 

     routes = validated_data.get('routes') 

     if routes: 
      for route_info in routes: 
       Route.objects.filter(id=route_info['id']).update(**route_info) 

     return instance 

希望它能幫助!

+0

不幸的是,其中'validated_data'包含'{'created_by':<用戶:[email protected]>,'total_bill_amount':704}',不包含'routes',因此它不會進入下一個「if」塊。 @Jahongir Rahmonov – RTan

+0

@ Md.TanvirRaihan你需要從'RouteSerializer'中刪除'read_only = True'(many = True,read_only = True)' –

+0

在刪除'read_only = True'後出現錯誤''.update()方法默認情況下不支持可寫嵌套字段。 爲串行化程序「conveyance.serializers.BillSerializer」寫入明確的'.update()'方法,或者在嵌套序列化程序字段中設置'read_only = True'。 ' – RTan

相關問題