django许多QuerySet'对象没有属性'成员'

发布于 2025-01-29 05:24:46 字数 881 浏览 2 评论 0原文

首先,这是我的位置模型。它有一个被称为成员的Manytomanyfield。

模型:

class Location(models.Model):
    name = models.CharField(max_length=200)
    # ...

    members = models.ManyToManyField(User, related_name="members")

    # ...

(请注意,“#...”替换了更多字段)

,然后在视图中做到了这一点。

详细信息视图

class LocationDetailView(DetailView):
    model = Location
    context_object_name = "location"
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        
        location = Location.objects.filter(pk=self.get_object().pk)
        context["members"] = location.members.all()
        
        return context

错误

'QuerySet' object has no attribute 'members'

First, here is my Location model. It has one ManyToManyField called members.

Model:

class Location(models.Model):
    name = models.CharField(max_length=200)
    # ...

    members = models.ManyToManyField(User, related_name="members")

    # ...

(Note, "# ..." replaces more fields)

And then in a view I did this.

DetailView

class LocationDetailView(DetailView):
    model = Location
    context_object_name = "location"
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        
        location = Location.objects.filter(pk=self.get_object().pk)
        context["members"] = location.members.all()
        
        return context

Error

'QuerySet' object has no attribute 'members'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

洋洋洒洒 2025-02-05 05:24:46

您的位置不是位置对象,而是包含一个位置对象的querySet。但是,不需要提取此问题,您可以使用:

class LocationDetailView(DetailView):
    model = Location
    context_object_name = 'location'
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['members'] = self.object.members.all()
        return context

但是实际上,您无需将成员传递给上下文:在模板中,您可以使用:

{% for member in location.members.all %}
    …
{% endfor %}

Your location is not a Location object, but a QuerySet that contains one Location object. Fetching this however is not necessary, you can work with:

class LocationDetailView(DetailView):
    model = Location
    context_object_name = 'location'
    
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['members'] = self.object.members.all()
        return context

But in fact you do not need to pass the members to the context: in the template you can work with:

{% for member in location.members.all %}
    …
{% endfor %}
油焖大侠 2025-02-05 05:24:46

我发现误会,

location = Location.objects.get(pk=self.get_object().pk)

不是

location = Location.objects.filter(pk=self.get_object().pk)

I found the misake, it's

location = Location.objects.get(pk=self.get_object().pk)

and not

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