如何在Django模型(其余框架)中返回图像文件?

时间:2018-11-05 07:23:34

标签: django python-3.x django-models django-rest-framework

Models.py文件 我的模特是

CREATE PROCEDURE dbo.insertdata (@t DATE)
AS
    INSERT INTO table2 (*columns*)
        SELECT * 
        FROM table1
        WHERE CAST(date AS DATE) = @t;
GO

Serializer.py文件 序列化器是:

class Club(models.Model):
    club_name = models.CharField(max_length=256, blank=True, null=True)
    *some other flieds*

class ClubRequirment(models.Model):
    *some other flieds*

class ClubImage(models.Model):
    image = models.ImageField(upload_to='club_image/picture/', blank=True, null=True)
    club = models.ForeignKey(Club, null=True, on_delete=models.CASCADE, related_name='club_image')
    created_on = models.DateTimeField(auto_now_add=True)
    update_on = models.DateTimeField(auto_now=True)

    def __str__(self):
        return str(self.image)
  

ClubSerializer的输出

class ClubImageSerializer(serializers.ModelSerializer):

    class Meta:
        model = ClubImage
        fields = "__all__"

class ClubRequirmentSerializer(serializers.ModelSerializer):

    class Meta:
       model = ClubRequirment
       fields = "__all__"

class ClubSerializer(serializers.ModelSerializer):
    club_requirment = serializers.SlugRelatedField(many=True,read_only=True,slug_field='text')
    club_image = serializers.StringRelatedField(many=True)

    class Meta:
        model = Club
        fields = ('id', 'club_name', 'club_image', 'club_requirment')

以上输出仅显示图像名称,我需要图像的完整路径。 是否有可能返回模型或任何解决方案

2 个答案:

答案 0 :(得分:1)

您已将club_image设置为StringRelatedField,因此将其视为字符串。您应该使用为图像编写的ModelSerializer。

class ClubSerializer(serializers.ModelSerializer):
    club_requirment = serializers.SlugRelatedField(many=True,read_only=True,slug_field='text')
    club_image = ClubImageSerializer(many=True)

    class Meta:
        model = Club
        fields = ('id', 'club_name', 'club_image', 'club_requirment')

如果您希望所有图像都位于同一阵列中

class ClubSerializer(serializers.ModelSerializer):
    club_requirment = serializers.SlugRelatedField(many=True,read_only=True,slug_field='text')
    club_image = ClubImageSerializer(many=True)

    class Meta:
        model = Club
        fields = ('id', 'club_name', 'club_image', 'club_requirment')

    def to_representation(self, instance):
        data = super().to_representation(instance)
        club_image = data.pop('club_image')
        club_image_array = [a.get('image') for a in club_image]
        data.update({'club_image': club_image_array})
        return data

答案 1 :(得分:1)

您可以按照文档中的说明使用SerializerMethodField()覆盖get_club_image方法以完成此操作。

views.py:

def club_view(request):
    obj = ClubImage.objects.all()
    club_serializer = ClubImageSerializer(obj, context={"request": request})
    ....

,然后在您的serializers.py

serializers.py:

class ClubImageSerializer(serializers.ModelSerializer):

    club_image = serializers.SerializerMethodField()

    class Meta:
      model = ClubImage
      fields = ("id","club","club_image")

    def get_club_image(self, obj):
      request = self.context.get('request')
      club_image = obj.image.url
      return request.build_absolute_uri(club_image)