Django ORM: Filter by extra attribute

前端 未结 3 1253
既然无缘
既然无缘 2020-12-03 17:44

I want to filter some database objects by a concatenated string.

The normal SQL query would be:

SELECT concat(firstName, \' \', name) FROM person WHE         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-03 18:31

    It's not a bug. filter() only inspects model definitions, so it doesn't recognize fullName as a declared field (because it's not - it's an extra argument in a query).

    You can add the fullName to WHERE using extra():

    Person.objects.extra(where=["fullName LIKE %s"], params=["Alexei%"])
    

提交回复
热议问题