搜尋

首頁  >  問答  >  主體

python - Django ModelSerializer 如何POST提交非Models中定義、關聯表中的欄位?

我在用djangorestframwork框架為app客戶端提供接口,現在遇到個問題如下,IWannaImages表外鍵IWanna表,現在我透過接口POST提交IWanna資料的時候無法獲取客戶端POST過來的到關聯表IwannaImages的訊息

這是我列印的客戶端提交的資料validated_data的內容:

{u'platform': u'ios', u'reason': u'\u5b81\u65e5\u8363\u6e7f\u7b54\u7b54\u53d1\u751f\u7684\u53d1\u7ed9\u6211\u7684', u'error_type': u'\u8f6f\u4ef6\u9519\u8bef', u'user_id': 56, u'wanna_type': u'\u7ea0\u9519\u8bef'}

從列印結果來看後台就沒接收到客戶端傳遞的images的數據,感覺是serializer這塊不對,接口這塊該如何處理呢?

models.py

class IWanna(models.Model):
    wanna_type = models.CharField(max_length=32, verbose_name=_("I wanna type"))
    scene_name = models.CharField(max_length=128, null=True, blank=True, verbose_name=_("scene name"))
    city_name = models.CharField(max_length=128, null=True, blank=True, verbose_name=_("city name"))
    reason = models.TextField(null=True, blank=True, verbose_name=_("wanna reason"))
    user_id = models.IntegerField(verbose_name=_("user id"))
    platform = models.CharField(max_length=32, verbose_name=_("platform"))
    error_type = models.CharField(max_length=128, null=True, blank=True, verbose_name=_("error type"))
    image = models.ImageField(upload_to=wanna_image_upload_to, null=True, blank=True, verbose_name=_("image"))
    improve_type = models.CharField(max_length=32, null=True, blank=True, verbose_name=_("improve type"))

    class Meta:
        app_label = 'people'
        verbose_name = _("I Wanna")
        verbose_name_plural = _("I Wanna")

    def __unicode__(self):
        return "{}-{}".format(self.id, self.wanna_type)


class IWannaImages(models.Model):
    iwanna = models.ForeignKey("IWanna", related_name="images", verbose_name=_("i wanna type"))
    image = models.ImageField(upload_to=wanna_image_upload_to, verbose_name=_("image"))

serializers.py

class IWannaSerializer(serializers.ModelSerializer):
    images = serializers.SerializerMethodField()

    class Meta:
        model = IWanna
        fields = (
            "id", "wanna_type", "scene_name", "city_name", "reason", "user_id",
            "platform", "error_type", "images", "improve_type", "image"
        )
        depth = 1

    def validate(self, attrs):
        if attrs["platform"] not in ["ios", "android"]:
            raise ValidationError({"platform": "platform not allowed"})
        return attrs

    def get_images(self, instance):
        data = IWannaImageSerializer(instance.images, many=True).data
        return data

    def create(self, validated_data):
        instance = super(IWannaSerializer, self).create(validated_data)
        try:
            if validated_data["images"]:   # traverse images from app
                for i in validated_data["images"]:
                    IWannaImages.objects.create(iwanna=instance, image=i)
        except Exception, e:
            import logging
            logging.warn(e.message)
        return instance
女神的闺蜜爱上我女神的闺蜜爱上我2719 天前1044

全部回覆(1)我來回復

  • 怪我咯

    怪我咯2017-06-22 11:53:46

    class IWannaSerializer(serializers.ModelSerializer):
        images = serializers.SerializerMethodField()
    
        class Meta:
            model = IWanna
            fields = (
                "id", "wanna_type", "scene_name", "city_name", "reason", "user_id",
                "platform", "error_type", "images", "improve_type", "image"
            )
            depth = 1
    

    class Meta多了depth=1,所以下級的清單被過濾了,你去掉試試!

    回覆
    0
  • 取消回覆