[英]DRF: Image vs Field naming
On my model, I have fields like photo = models.ImageField()
– practically this means I can upload and set images from the admin;在我的模型上,我有像
photo = models.ImageField()
这样的字段——实际上这意味着我可以从管理员上传和设置图像; but from the DRF Serializer it feels more appropriate to suffix URL: photo_url = serializers.ImageField()
但是从 DRF Serializer 感觉后缀 URL 更合适:
photo_url = serializers.ImageField()
Is there a standard way of doing this in a DRF-y way here?这里有以 DRF-y 方式执行此操作的标准方法吗? I could of course make the
photo
on my model into photo_url
, but that feels a bit strange.我当然可以将模型上的
photo
变成photo_url
,但这感觉有点奇怪。
You can do it in serializer validate method.您可以在序列化程序验证方法中执行此操作。 For example:
例如:
class YourSerializer(serializers.ModelSerializer):
photo_url = serializers.ImageField()
class Meta:
model = YourModel
fields = [] #your model fields
def validate(self,attrs):
attrs['photo'] = attrs['photo_url']
return attrs
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.