Django ORM: Filter by extra attribute

前端 未结 3 1241
既然无缘
既然无缘 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条回答
  •  萌比男神i
    2020-12-03 18:11

    The proposed solution worked great with postgresql and JSONB fields in the code below. Only records that have the 'partner' key under the 'key' jsonb field are returned:

    query_partner = "select key->>'partner' from accounting_subaccount " \
                    "where accounting_subaccount.id = subaccount_id and key ? 'partner'"
    qs = queryset.extra(select={'partner': query_partner}, where=["key ? 'partner'"])
    

提交回复
热议问题