使用Django REST框架的通用ListCreateAPIView,我创建了一个终点,我认为应该能够通过POST请求上传照片.我正在为本教程建模我的代码.
到目前为止,我已尝试使用Android和curl将文件POST到此端点,并观察到相同的行为:创建了一条新记录,但未附加该文件.由于该文件是必填字段,因此服务器返回500错误.
这个问题看起来很相似,但他没有使用REST框架的通用视图,我不知道为什么......我想在适用的地方利用它们.
这是我的Django视图:
class PhotoList(generics.ListCreateAPIView):
model = Photo
serializer_class = PhotoSerializer
permission_classes = [
permissions.AllowAny
]
Run Code Online (Sandbox Code Playgroud)
......我的模特:
def get_unique_image_file_path(instance=None, filename='dummy.jpg'):
"""
function to determine where to save images. assigns a uuid (random string) to each and places it
in the images subdirectory below media. by default, we assume the file is a .jpg
"""
ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
# TODO: 'images' is hard coded
return os.path.join('images', filename)
class …Run Code Online (Sandbox Code Playgroud)