2017-05-21 74 views
2

我如何獲得產品名稱和id而不是pro_id和ord_id輸出?但不是字符串類型。例如:「name:somebook」對我來說不是一個有效的選擇。 我只是在這裏工作了2天,沒有休息,我想我缺少一點細節,但我找不到它是什麼。我如何使用DRF序列化manytomany django模型

輸出我想

[ 
    { 
"order_id": 1, 
"totalquantity": 12, 
"totalprice": 56, 
"userid": 1, 
"customerAddress": "evka1", 
"customerPhone": "539", 
"trackNo": 12034, 
"products": [ 
    { 
    "name": "somebook", 
    "id": 1, 
    "quantity": 6 
    }, 
    { 
    "name": "someotherbook", 
    "id": 2, 
    "quantity": 6 
    } 
] 
    } 
] 

輸出I得到

[ 
    { 
"order_id": 1, 
"totalquantity": 12, 
"totalprice": 56, 
"userid": 1, 
"customerAddress": "evka1", 
"customerPhone": "539", 
"trackNo": 12034, 
"products": [ 
    { 
    "pro_id": 2, 
    "ord_id": 1, 
    "quantity": 6 
    }, 
    { 
    "pro_id": 3, 
    "ord_id": 1, 
    "quantity": 6 
    } 
] 
    } 
] 

訂貨型號

class Order(models.Model): 

    order_id = models.AutoField(primary_key=True) 
    totalquantity = models.IntegerField(default=0, null=True) 
    totalprice = models.IntegerField(default=0, null=True) 
    userid = models.IntegerField(default=0, null=True) 
    trackNo = models.IntegerField(default=0, null=True) 
    billNo = models.IntegerField(default=0, null=True) 
    customerAddress = models.CharField(max_length=30,default="nil", null=True) 
    customerPhone = models.CharField(max_length=30,default="nil", null=True) 

順序串行

class OrderSerializer(serializers.ModelSerializer): 
products = ProductOrderSerializer(many=True, read_only=True) 

class Meta: 
    model = Order 
    fields = ('order_id', 'totalquantity', 'totalprice', 'userid', 'customerAddress', 'customerPhone', 'trackNo', 'products') 

產品型號

class Product(models.Model): 

    name = models.CharField(max_length=30,default="nil", null=True) 
    author = models.CharField(max_length=30,default="nil", null=True) 
    date = models.DateField(null=True) 
    price = models.FloatField(default=0, null=True) 
    quantity = models.IntegerField(default=0, null=True) 
    soldcount = models.IntegerField(default=0, null=True) 
    category = models.CharField(max_length=30,default="nil", null=True) 

產品串行

class ProductSerializer(serializers.ModelSerializer): 
class Meta: 
    model = Product 
    fields = ('id', 'name', 'author', 'date', 'price', 'quantity', 'soldcount', 'category') 

ProductOrder型號

class ProductOrder(models.Model): 

    pro_id = models.IntegerField(default=0,null=True) 
    ord_id = models.ForeignKey(Order, related_name='products') 
    quantity = models.IntegerField(default=0, null=True) 

ProductOrder串行

class ProductOrderSerializer(serializers.ModelSerializer): 
class Meta: 
    model = ProductOrder 
    fields = ('pro_id', 'ord_id', 'quantity') 

回答

0

您需要的輸出可以通過django-rest-framework中的嵌套序列化器來實現,

但是,它需要你的模型一些重構,

你需要一個ForeignKey添加到模型ProductOrder對產品型號,序達到一個多對多的關係,

class ProductOrder(models.Model): 
    pro_id = models.ForeinKey(Product, related_name='orders', default=0,null=True) 
    ord_id = models.ForeignKey(Order, related_name='products') 
    quantity = models.IntegerField(default=0, null=True) 

而且,在你的序列化,

class ProductSerializer(serializers.ModelSerializer): 
    class Meta: 
     model = Product 
     fields = ('name', 'id', 'quantity') 


class ProductOrderSerializer(serializers.ModelSerializer): 
    details = ProductSerializer(source='pro_id', read_only=True) 

    class Meta: 
     model = ProductOrder 
     fields = ('details',) 


class OrderSerializer(serializers.ModelSerializer): 
    products = ProductOrderSerializer(many=True, read_only=True) 

    class Meta: 
     model = Order 
     fields = ('totalquantity', 'totalprice', 'userid', 
       'trackNo', 'billNo', 'customerAddress', 'customerPhone', 
       'products') 

您能夠打電話OrderSerializer,正如你所期望得到的輸出,

def get(self, request, *args, **kwargs): 
    orders = Order.objects.all() 
    serializer = OrderSerializer(orders, many=True) 
    return Response(serializer.data) 

的輸出將是這樣的,

[ 
    { 
     "totalquantity": 0, 
     "totalprice": 0, 
     "userid": 0, 
     "trackNo": 0, 
     "billNo": 0, 
     "customerAddress": "nil", 
     "customerPhone": "nil", 
     "products": [ 
      { 
       "details": { 
        "name": "nil", 
        "id": 1, 
        "quantity": 0 
       } 
      }, 
      { 
       "details": { 
        "name": "nil", 
        "id": 1, 
        "quantity": 0 
       } 
      } 
     ] 
    } 
] 
0

我解決了這個問題,當我改變ProductOrderSerializer到:在 「我的代碼工作,我不知道爲什麼,」 現在的情況

class ProductListingSerializer(serializers.RelatedField): 

    def to_representation(self, value): 

     dict={} 
     dict['name'] = value.pro_id.name 
     dict['id'] = value.pro_id.pk 
     dict['quantity'] = value.quantity 
     return dict 

,但即時通訊: D

相關問題