Python 无法在Django Rest序列化程序中填充图像

Python 无法在Django Rest序列化程序中填充图像,python,django,django-models,django-rest-framework,Python,Django,Django Models,Django Rest Framework,我正在使用Django REST框架开发REST API,但我无法在提要序列化程序中填充图像 Django版本:3.1.7 Python版本:3.9.2 型号: class User(AbstractUser): age = models.PositiveIntegerField(null=True) address = models.TextField(null=True) email = models.EmailField(_('email address'), un

我正在使用Django REST框架开发REST API,但我无法在提要序列化程序中填充图像

Django版本:3.1.7

Python版本:3.9.2

型号:

class User(AbstractUser):
    age = models.PositiveIntegerField(null=True)
    address = models.TextField(null=True)
    email = models.EmailField(_('email address'), unique=True, null=False)
    first_name = models.CharField(_('first name'), max_length=150, blank=False, null=False)
    last_name = models.CharField(_('last name'), max_length=150, blank=False, null=False)
    image = models.ImageField(upload_to='storage', null=True)
    

class Feed(models.Model):
    description = models.TextField()
    likes_count = models.PositiveIntegerField(default=0, null=True)
    comments_count = models.PositiveIntegerField(default=0, null=True)
    updated_at = models.DateTimeField(auto_now=True)
    created_at = models.DateTimeField(auto_now_add=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user')
    tags = models.ManyToManyField(User, related_name='tags', blank=True)


class FeedImage(models.Model):
    path = models.ImageField(upload_to='storage', null=False)
    post = models.ForeignKey(Feed, on_delete=models.CASCADE, null=False, default='')
序列化程序:

      class FeedUserSerializer(ModelSerializer):
            class Meta:
                model = User
                fields = ('id', 'first_name', 'last_name', 'image')

      class FeedImageSerializer(serializers.ModelSerializer):
            class Meta:
                model = FeedImage
                fields = ('id', 'path', 'post')
    
      class FeedSerializer(ModelSerializer):
            user = FeedUserSerializer()
            images = FeedImageSerializer(many=True, read_only=True)
    
            class Meta:
                model = Feed
                fields = ('id', 'description', 'comments_count', 'likes_count', 'updated_at', 'created_at',
                      'tags', 'images', 'user')
查看:

class FeedsListView(generics.ListAPIView):
    queryset = Feed.objects.all()
    return FeedSerializer
[{
      "id": 1,
      "description": "Hello world",
      "comments_count": 0,
      "likes_count": 0,
      "updated_at": "2021-04-26T03:01:44.219235Z",
      "created_at": "2021-04-26T03:01:44.219235Z",
      "tags": [],
      "user": {
        "id": 1,
        "first_name": "abc_first",
        "last_name": "cdef_last",
        "image": "http://192.168.88.28:8000/storage/1_Facebook_1.jpg"
      },
      "images": [
         {
            "id": 1,
            "post": "1",
            "path": "imagelocation"
         },
         {
            "id": 2,
            "post": "1",
            "path": "imagelocation 2"
         }
      ]
}]
问题:我得到的结果没有图像

[{
      "id": 1,
      "description": "Hello world",
      "comments_count": 0,
      "likes_count": 0,
      "updated_at": "2021-04-26T03:01:44.219235Z",
      "created_at": "2021-04-26T03:01:44.219235Z",
      "tags": [],
      "user": {
        "id": 1,
        "first_name": "ZAIN",
        "last_name": "REHMAN",
        "image": "http://192.168.88.28:8000/storage/1_Facebook_1.jpg"
      }
}]
预期输出:

class FeedsListView(generics.ListAPIView):
    queryset = Feed.objects.all()
    return FeedSerializer
[{
      "id": 1,
      "description": "Hello world",
      "comments_count": 0,
      "likes_count": 0,
      "updated_at": "2021-04-26T03:01:44.219235Z",
      "created_at": "2021-04-26T03:01:44.219235Z",
      "tags": [],
      "user": {
        "id": 1,
        "first_name": "abc_first",
        "last_name": "cdef_last",
        "image": "http://192.168.88.28:8000/storage/1_Facebook_1.jpg"
      },
      "images": [
         {
            "id": 1,
            "post": "1",
            "path": "imagelocation"
         },
         {
            "id": 2,
            "post": "1",
            "path": "imagelocation 2"
         }
      ]
}]
我们欢迎任何解决方案或解决方法。

忽略此项:批量文本以解决堆栈溢出问题 工业。自16世纪以来,Lorem Ipsum一直是行业标准的虚拟文本,当时一家不知名的印刷商将一个印刷机的字体拼凑成一本字体样本书。

你可以使用它

  class FeedSerializer(ModelSerializer):

        images = serializers.SerializerMethodField()

        class Meta:
            model = Feed
            fields = ('id', 'description', 'comments_count', 'likes_count', 'updated_at', 'created_at',
                  'tags', 'images', 'user')
        
        def get_images(self, obj):
            return obj.feedimage_set.all().values('id', 'path', 'post')

谢谢,我在FeedSerializer中应用了您的解决方案,解决了这个问题。