django 视图 - 在通用视图中访问 m2m 字段
我偶然发现了这个问题,我的菜鸟大脑在试图解决它时被炸了。我觉得这里缺少一些基本概念。
因此,我有一个带有类别选择字段的“电影”模型,以及与“导演”模型的 m2m 关系,我正在尝试编写 2 个不同的视图,一个返回按类别过滤的电影列表,另一个返回列表由导演过滤的电影。 第一个很简单,但我只是不知道如何获取导演模型的名称字段来创建第二个过滤器。
所以我有这个模型(我已经去掉了不相关的东西,包括我上面提到的类别)
class Director(models.Model):
name = models.CharField(max_length=50)
web = models.URLField(blank=True, help_text= "opcional")
class Film(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length= 15)
director = models.ManyToManyField(Director, blank=True, help_text= "opcional")
这个url
(r'^peliculas/director/(?P<director>\w+)/$', 'filtered_by_director'),
和这个视图
def filtered_by_director(request,director):
return list_detail.object_list(
request,
queryset = Film.objects.filter(director.name=director),
template_name ='sections/film_list.html',
template_object_name = 'film',
paginate_by = 3
)
两个视图应该使用相同的模板来渲染相关的对象列表 该视图不喜欢我在 m2m 字段的查询集中使用的过滤器,但我不知道如何真正做到这一点,我已经尝试了我能想到的一切,它给了我一个“关键字不能”是一个表达式”错误
任何对这个低级菜鸟的帮助将不胜感激。
I've stumbled upon this issue and my noob brain got fried trying to resolve it. I feel like there's some basic concepts here that I'm missing.
So I have this "Films" model with category choice field and a m2m relationship to a "Directors" model, and I'm trying to write 2 different views, one that returns a list of films filtered by category and one that returns a list of films filtered by director.
The first one is easy, but I just don't know how to get the director model's name field to create the second filter.
So I have this models (i've taken the irrelevant stuff out including the category thing i mentioned above)
class Director(models.Model):
name = models.CharField(max_length=50)
web = models.URLField(blank=True, help_text= "opcional")
class Film(models.Model):
name = models.CharField(max_length=50)
slug = models.SlugField(max_length= 15)
director = models.ManyToManyField(Director, blank=True, help_text= "opcional")
this url
(r'^peliculas/director/(?P<director>\w+)/
and this view
def filtered_by_director(request,director):
return list_detail.object_list(
request,
queryset = Film.objects.filter(director.name=director),
template_name ='sections/film_list.html',
template_object_name = 'film',
paginate_by = 3
)
The same template is supposed to be used by both views to render the relevant list of objects
The view doesn't like the filter i'm using at the queryset for the m2m field, but I have no clue how to do it really, I've tried whatever I could think of and it gives me a "keyword can't be an expression" error
Any help to this lowly noob will be appreciated.
, 'filtered_by_director'),
and this view
The same template is supposed to be used by both views to render the relevant list of objects
The view doesn't like the filter i'm using at the queryset for the m2m field, but I have no clue how to do it really, I've tried whatever I could think of and it gives me a "keyword can't be an expression" error
Any help to this lowly noob will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
queryset = Film.objects.filter(director.name=director),
行需要读取:
queryset = Film.objects.filter(director__name=director),
字段查找为通过
__
双下划线语法完成:http://docs.djangoproject.com/en/dev/主题/db/queries/#field-lookups
Line
queryset = Film.objects.filter(director.name=director),
needs to read:
queryset = Film.objects.filter(director__name=director),
Field lookups are done by
__
double underscore syntax:http://docs.djangoproject.com/en/dev/topics/db/queries/#field-lookups
在您的过滤器中,尝试指定主管名称,例如 (文档):
filter(director__name=director)
In your filter, try specifying the director name like (documentation):
filter(director__name=director)