toolshed/backend/files/serializers.py
jedi d00d637dca
All checks were successful
continuous-integration/drone/push Build is passing
add /files/ and /item_files/ API endpoints
2023-07-07 19:31:50 +02:00

20 lines
651 B
Python

from django.core.files.base import ContentFile
from rest_framework import serializers
from files.models import File
class FileSerializer(serializers.Serializer):
data = serializers.CharField()
mime_type = serializers.CharField()
class Meta:
model = File
fields = ['data', 'mime_type']
read_only_fields = ['id', 'size', 'name']
def to_representation(self, instance):
return {'id': instance.id, 'name': instance.file.url, 'size': instance.file.size,
'mime_type': instance.mime_type}
def create(self, validated_data):
return File.objects.get_or_create(**validated_data)[0]