Calling filter with a variable for field name

前端 未结 2 1197
夕颜
夕颜 2021-02-05 03:14

Is there a way to call filter on a queryset where one of the fieldnames is a variable?

For example I have something like:

models.py

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-05 03:47

    To use field name string with icontains.

    Try this

    field_name = 'video'
    field_name_icontains = field_name + '__icontains'
    Playlist.objects.filter(**{field_name_icontains: v})
    

提交回复
热议问题