Django Queryset: Cast VARCHAR field in SQL before filtering

前端 未结 2 570
旧巷少年郎
旧巷少年郎 2021-01-24 05:34

I have a table that I cannot control, but need to select from it. The field \"building\" is a varchar, and also defined like this in my (non managed) django model. But it should

相关标签:
2条回答
  • 2021-01-24 06:11

    as simple as it gets: use a regex query

    regex = r'^0{0,4}%s ?$' % building_no
    address = Address.objects.filter(building__iregex=regex)\
    

    any hints on performance welcome...

    0 讨论(0)
  • 2021-01-24 06:14

    try simple int(you string value)

    for example:

    str = "00100"
    int(str)
    

    will give 100 as integer value. same applies for

     str = "100100"
    int(str)
    

    gives 100100 as integer

    0 讨论(0)
提交回复
热议问题