使用字段名称的变量调用过滤器 [英] Calling filter with a variable for field name

查看:93
本文介绍了使用字段名称的变量调用过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



有一种方法可以在一个字段名称是变量的queryset上调用过滤器吗?例如,我有一些类似于:



models.py

 类播放列表(models.Model):
video = ...

views.py

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

当然会导致错误 field_name 不是播放列表可以

解决方案

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


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.

解决方案

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

这篇关于使用字段名称的变量调用过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆