1
DRF默認序列化一個filefield或imagefield路徑到它的相對路徑。Django REST Framework和FileField絕對URL位於同一字段
如在此質疑Django REST Framework and FileField absolute url
我知道這是可能產生所謂的即「FILE_URL」自定義字段和序列化的完整路徑。
但是有可能序列化它在同一領域?像:
class Project(models.Model):
name = models.CharField(max_length=200)
thumbnail = models.FileField(upload_to='media', null=True)
class ProjectSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Project
fields = ('id' ,'url', 'name', 'thumbnail')
class ProjectViewSet(viewsets.ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
{
"id": 1,
"url": "http://localhost:8000/api/v1/projects/1/",
"name": "Institutional",
"thumbnail": "ABSOLUTE URL"
}
謝謝,就像一個冠軍 – lapinkoira
@lapinkoira歡迎您 –