search

Home  >  Q&A  >  body text

python - How does Django ModelSerializer POST submit fields that are not defined in Models and in related tables?

I am using the djangorestframework framework to provide an interface for the app client. Now I encounter a problem as follows. The IWannaImages table has a foreign key to the IWanna table. Now when I submit IWanna data through the interface POST, I cannot get the associated table IwannaImages from the client POST. Information

This is the content of the validated_data submitted by the client that I printed:

{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'}

Judging from the printing results, the background did not receive the images data passed by the client. I feel that the serializer is wrong. How to deal with the interface?

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
女神的闺蜜爱上我女神的闺蜜爱上我2716 days ago1037

reply all(1)I'll reply

  • 怪我咯

    怪我咯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 has an additional depth=1, so the lower-level list is filtered. Try removing it!

    reply
    0
  • Cancelreply