从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将自动使用两种格式副本进行处理:smallmedium

要访问它们,请使用:

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}}}}}}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java有没有工具可以将zephyr转换为velocity模板?   java在安卓 studio中从JSON响应中获取值   jvm如何在Java中设计一个好的permgen空间字符串?   java如何防止Rest webservice使用被盗令牌进行身份验证   java无法遍历列表JSTL   找不到用于ResourceServerTokenServices的java Bean SpringSecurityOauth2   java子字符串替换问题   爪哇玻璃鱼3。十、 以编程方式处理任意HTTPSession的终止   java如何检查输入是否为整数,并在最后添加一个命令来重新启动while循环?   引发java ical4j 1.0.6不可解析日期异常   Java等价于Delphi的DBCtrlGrid?   如果发生错误,java将查找下一个预期标记ANTLR 3   java自打开应用程序(创建锁屏)   java为什么netty有自己的ConcurrentHashMap?   Gradle任务中的java拉取和运行依赖项   继承与Java继承的混淆   java使用shell脚本中的版本执行jar   java我无法让Sqlite数据库与带有Maven的JavaFX应用程序IDE Eclipse包正确通信   java控制台日志未通过org打印。阿帕奇。hadoop。mapreduce。作业的waitForCompletion(true)方法   JAVAlang.NoSuchMethodError:apachestorm螺栓中的spring getrequest