我偶然发现了这个问题,我的菜鸟大脑试图解决它。我觉得这里缺少一些基本概念。
所以我有这个带有类别选择字段的“电影”模型和与“导演”模型的 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")
这个网址
(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
)
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.