<small id='CoyAo'></small><noframes id='CoyAo'>

    1. <tfoot id='CoyAo'></tfoot>

        <bdo id='CoyAo'></bdo><ul id='CoyAo'></ul>

      <legend id='CoyAo'><style id='CoyAo'><dir id='CoyAo'><q id='CoyAo'></q></dir></style></legend>

      <i id='CoyAo'><tr id='CoyAo'><dt id='CoyAo'><q id='CoyAo'><span id='CoyAo'><b id='CoyAo'><form id='CoyAo'><ins id='CoyAo'></ins><ul id='CoyAo'></ul><sub id='CoyAo'></sub></form><legend id='CoyAo'></legend><bdo id='CoyAo'><pre id='CoyAo'><center id='CoyAo'></center></pre></bdo></b><th id='CoyAo'></th></span></q></dt></tr></i><div id='CoyAo'><tfoot id='CoyAo'></tfoot><dl id='CoyAo'><fieldset id='CoyAo'></fieldset></dl></div>

        在 Django ORM 中查询时如何将字符转换为整数?

        时间:2023-06-02

          <tbody id='APNSH'></tbody>
          <bdo id='APNSH'></bdo><ul id='APNSH'></ul>
          <i id='APNSH'><tr id='APNSH'><dt id='APNSH'><q id='APNSH'><span id='APNSH'><b id='APNSH'><form id='APNSH'><ins id='APNSH'></ins><ul id='APNSH'></ul><sub id='APNSH'></sub></form><legend id='APNSH'></legend><bdo id='APNSH'><pre id='APNSH'><center id='APNSH'></center></pre></bdo></b><th id='APNSH'></th></span></q></dt></tr></i><div id='APNSH'><tfoot id='APNSH'></tfoot><dl id='APNSH'><fieldset id='APNSH'></fieldset></dl></div>
          <tfoot id='APNSH'></tfoot>
          1. <small id='APNSH'></small><noframes id='APNSH'>

            • <legend id='APNSH'><style id='APNSH'><dir id='APNSH'><q id='APNSH'></q></dir></style></legend>

                  本文介绍了在 Django ORM 中查询时如何将字符转换为整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  最近开始使用Django ORM.我想执行这个查询

                   select student_id from student_id like "%97318%" order by CAST(student_id as UNSIGNED) desc;

                  其中 student_id 是一个 CharField,我希望将其作为整数进行查询.我试过

                   student.objects.filter(student_id__contains "97318").order('-student_id')

                  工作正常.但是不知道也找不到如何将student_id"转换为 int 就像上面提到的Django ORM"的实际 MySQL 查询一样.我应该使用原始查询还是有出路?让我知道您的建议.

                  解决方案

                  无需使用 extra 的更新替代方案是 cast 函数(Django 1.10 中的新功能):

                  <预><代码>>>>从 django.db.models 导入 FloatField>>>从 django.db.models.functions 导入 Cast>>>Value.objects.create(整数=4)>>>value = Value.objects.annotate(as_float=Cast('integer', FloatField())).get()>>>>打印(value.as_float)4.0

                  来自 https://docs.djangoproject.com/en/1.10/ref/models/database-functions/#cast

                  Recently started using Django ORM.I want to execute this query

                   select student_id from students where student_id like "%97318%" order by CAST(student_id as UNSIGNED) desc;   
                  

                  where student_id is a CharField which I want as integer for querying. I tried with

                    students.objects.filter(student_id__contains "97318").order('-student_id')
                  

                  works fine. But don't know and couldn't find how to cast "student_id" to int like the actual MySQL query mentioned above with "Django ORM". should I use raw query or is there a way out? Let me know your suggestions.

                  解决方案

                  An updated alternative without requiring the use of extra is the cast function (new in Django 1.10):

                  >>> from django.db.models import FloatField
                  >>> from django.db.models.functions import Cast
                  >>> Value.objects.create(integer=4)
                  >>> value = Value.objects.annotate(as_float=Cast('integer', FloatField())).get()> 
                  >>> print(value.as_float)
                  4.0
                  

                  From https://docs.djangoproject.com/en/1.10/ref/models/database-functions/#cast

                  这篇关于在 Django ORM 中查询时如何将字符转换为整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:MySql 一次更新两个表 下一篇:MySQL 将 timediff 输出转换为日、时、分、秒格式

                  相关文章

                • <small id='wzS8z'></small><noframes id='wzS8z'>

                  <i id='wzS8z'><tr id='wzS8z'><dt id='wzS8z'><q id='wzS8z'><span id='wzS8z'><b id='wzS8z'><form id='wzS8z'><ins id='wzS8z'></ins><ul id='wzS8z'></ul><sub id='wzS8z'></sub></form><legend id='wzS8z'></legend><bdo id='wzS8z'><pre id='wzS8z'><center id='wzS8z'></center></pre></bdo></b><th id='wzS8z'></th></span></q></dt></tr></i><div id='wzS8z'><tfoot id='wzS8z'></tfoot><dl id='wzS8z'><fieldset id='wzS8z'></fieldset></dl></div>

                • <tfoot id='wzS8z'></tfoot>

                    <bdo id='wzS8z'></bdo><ul id='wzS8z'></ul>

                      <legend id='wzS8z'><style id='wzS8z'><dir id='wzS8z'><q id='wzS8z'></q></dir></style></legend>