Files
funnywolf b6b4cbf017 0.4.0 I always have a choice
You shape nothing until you hold everything.
2026-06-27 19:08:48 +08:00

46 lines
1.1 KiB
Python

from django.urls import reverse
from rest_framework import serializers
from .models import Attachment
class AttachmentFileField(serializers.FileField):
def get_attribute(self, instance):
return instance
def to_representation(self, attachment):
if not attachment.file:
return None
url = reverse(
"attachment-download",
kwargs={"access_key": attachment.access_key},
)
return url
class AttachmentSerializer(serializers.ModelSerializer):
file = AttachmentFileField()
uploaded_by_name = serializers.CharField(source="uploaded_by.username", read_only=True)
class Meta:
model = Attachment
fields = (
"id",
"access_key",
"file",
"filename",
"size",
"uploaded_by",
"uploaded_by_name",
"uploaded_at",
)
read_only_fields = (
"id",
"access_key",
"filename",
"size",
"uploaded_by",
"uploaded_at",
)