从django url字段创建自定义格式副本
django-url-renditions的Python项目详细描述
django url格式副本
django url格式副本允许从django url字段定义和呈现格式副本。这对于django-s3direct字段也非常有效。
它带有一个grapheneQuery
,可以在graphql模式中启用格式副本。
安装
pip install django-url-renditions
将url_renditions
添加到INSTALLED_APPS
定义您的模型
fromdjango.dbimportmodelsfromurl_renditions.fieldsimportFileUrlWithRenditions# django-url-renditions comes with a simple image resizer method.# Look at the implementation if you need something more specificfromurl_renditions.resize_imageimportResizeImageclassTrack(models.Model):original_artwork=models.URLField()artwork=FileUrlWithRenditions(source='original_artwork',use_job_runner=True,# if we want to queue the job with django_rqrenditions={'small':ResizeImage('80x80'),'medium':ResizeImage('300x300'),})
这样,当使用original_artwork
创建Track
模型时,artwork
将自动使用两种格式副本进行处理:small
和medium
。
要访问它们,请使用:
r=my_track.artwork.rendition_set.get(name='small')print('url:',r.href,'width:',r.width,'height:',r.height)
石墨烯图形ql
将url_renditions.graphql_schema.Query
添加到根查询。
importgrapheneimporturl_renditions.graphql_schema# noqaclassQuery(...url_renditions.graphql_schema.Query,graphene.ObjectType):passschema=graphene.Schema(query=Query)
然后当您要求
{ track(id: "VHJhY2s6OA==") { artwork { renditions { medium { href width height } } } } }
您将得到
{"data":{"track":{"artwork":{"renditions":{"medium":{"href":"https://images.unsplash.com/photo-1474314170901-f351b68f544f","width":300,"height":300}}}}}}