可浏览的django-reset-framework提供了一个方便的"原始数据"选项卡,其中包含一个空-json,可以轻松创建新对象.
我有许多不同的Serializer,每个请求类型(GET/POST)和其他逻辑更改,所以我想测试这个empty-json作为我的单元测试的一部分.
我的问题是:如何基于APIClient请求(drf的测试客户端)以编程方式检索序列化程序?
我在https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/renderers.py#L454上看到了代码,具体来说,
view.get_serializer(instance=obj)
Run Code Online (Sandbox Code Playgroud)
但我仍然无法从APIClient构建一个序列化器.
我有一个使用easy_thumbnails(| thumbnail_url)显示的ImageField模型.
我的问题是如果ImageField为空,我如何显示默认图像?
我想在模型/视图中使用此逻辑,而不是在html /模板中.
例如:
DEFAULT_PICTURE = 'default.jpg'
def get_picture(self):
if self.picture:
return self.picture
else:
from DEFAULT_PICTURE
Run Code Online (Sandbox Code Playgroud)
get_picture()返回哪个对象与easy_thumbnails兼容?
我尝试创建一个新的File对象,就像这里一样,但它没有用.
您能否提供一个返回现有文件以使用easy_thumbnails显示的工作示例?