How do i cast char to integer while querying in django ORM?

后端 未结 3 1798
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 11:03

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 a         


        
3条回答
  •  日久生厌
    2020-11-28 11:19

    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

提交回复
热议问题