django 视图 - 在通用视图中访问 m2m 字段

发布于 2024-10-17 19:45:26 字数 1167 浏览 2 评论 0原文

我偶然发现了这个问题,我的菜鸟大脑在试图解决它时被炸了。我觉得这里缺少一些基本概念。

因此,我有一个带有类别选择字段的“电影”模型,以及与“导演”模型的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抠脚大汉 2024-10-24 19:45:26

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

淡淡绿茶香 2024-10-24 19:45:26

在您的过滤器中,尝试指定主管名称,例如 (文档):

filter(director__name=director)

In your filter, try specifying the director name like (documentation):

filter(director__name=director)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文