Calling filter with a variable for field name

前端 未结 2 1195
夕颜
夕颜 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:27

    Playlist.objects.filter(**{field_name: v})

    0 讨论(0)
  • 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})
    
    0 讨论(0)
提交回复
热议问题