如何序列化现有图像以保存在 models.ImageField 参数中?

How to serialize an already existing image to save in a models.ImageField param?

我想使用 Django Rest Framework 创建 Note 对象,其中模型的字段之一是 ImageField。 我已经可以使用 API 创建对象并更新所有不同的字段,图像除外。

我的代码:

models.py

class Note(OwnedModel):
    note_id = models.UUIDField(primary_key=True,
                               default=uuid.uuid4,
                               editable=False)
    # note_owner = models.ForeignKey(, null=True, blank=True, on_delete=models.SET_NULL)
    note_name = models.CharField(max_length=50)
    body = models.TextField()
    updated = models.DateTimeField(auto_now=True)
    created = models.DateTimeField(auto_now_add=True)
    qr_image        = models.ImageField(upload_to='notes', null=True)


    def __str__(self):
        return self.note_name[0:50]

    class Meta:
        ordering = ['-updated']

views.py

@api_view(['GET', 'POST'])
def getNote(request, pk=None):
    if request.method == 'GET':
        note = Note.objects.get(note_id=pk)
        serializer = NoteSerializer(note, many=False)
        return Response(serializer.data)

    elif request.method == 'POST':

        _data = request.data.copy()
        owner = request.user.id
        _data["owner"] = owner

        # Generate QR code
        qr_image = generate_qr(_data["note_name"])

        # HOW TO PASS THE IMAGE TO THE SERIALIZER?
        _data["qr_image"] = qr_image
        # _data["qr_image"] = qr_image[0]
        # _data["qr_image"] = qr_image[1]


        serializer = NoteSerializer(data=_data)
        
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response(status=status.HTTP_400_BAD_REQUEST)

serializers.py

class NoteSerializer(ModelSerializer):
    class Meta:
        model = Note
        fields = '__all__'

qr_code.py

import qrcode

def generate_qr(qr_file_name=None):

    qr = qrcode.QRCode(
        version=1,
        # error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=4,
        border=3,
    )
    qr.add_data(qr_file_name)
    qr.make(fit=True)
    img = qr.make_image()
    # img = qr.make_image(fill_color="black", back_color="white")
    path='images/notes/'+str(qr_file_name)+'.jpg'
    img.save(path)

    return path, img

二维码生成正确,完美保存在上传路径

我无法正确构建行 _data["qr_image"] = qr_image,或者如果我需要以不同于 generate_qr 函数的方式 return 图像。其他一切都运行良好(例如从管理员创建 Note 对象并上传二维码图像)。

@api_view(['GET', 'POST'])
def getNote(request, pk=None):
    if request.method == 'GET':
        # automatically raise 404 if obj does not exist
        note = get_object_or_404(Note.objects, note_id=pk)

        # many=False is default, dont need that
        serializer = NoteSerializer(note)
        return Response(serializer.data)

    elif request.method == 'POST':
        serializer = NoteSerializer(data=request.data)

        # raise_exception=True will raise validation error (400) automatically
        serializer.is_valid(raise_exception=True)
        
        # get note name after validation to be sure it has proper length etc.
        qr_image = generate_qr(serializer.validated_data.get("note_name"))

        # attributes provided here will bypass validation
        # and will be injected directly to model create method
        serializer.save(
            owner=request.user,
            qr_image=qr_image
        )
        return Response(serializer.data, status=status.HTTP_201_CREATED)

我不确定您的 generate_qr 函数是否应该将图像保存在给定路径中,因为 Note 应该在创建时为您完成。

提示:检查 viewsets.GenericViewSet class - 它会正确地将您的视图拆分为方法,让生活更轻松。