mak*_*aki 1 django django-queryset django-annotate
我有我需要得到所有对象,其中使用案例existing_field是开始some string。
some string 动态变化,所以我需要一种聪明的方法来过滤掉对象。
我的想法是像这样创建带注释的查询:
MyModel.objects.annotate(annotated_field='some string').filter(annotated_field__startswith=F('existing_field'))
Run Code Online (Sandbox Code Playgroud)
目前它失败了:
QuerySet.annotate() received non-expression(s): some string
有没有办法用字符串值注释对象?
kyo*_*ore 10
不确定您在问什么,但请尝试使用Value表达式。
MyModel.objects.annotate(annotated_field=Value('some string', output_field=CharField())).filter(annotated_field__startswith=F('existing_field'))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2066 次 |
| 最近记录: |