Calling filter with a variable for field name

2019-03-14 17:45发布

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

class Playlist(models.Model):
    video = ...

views.py

field_name = 'video'
Playlist.objects.filter(field_name=v)

Which of course results in an error that field_name is not an attribute upon which Playlist can be filtered.

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-03-14 18:34

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

查看更多
登录 后发表回答