django许多QuerySet'对象没有属性'成员'
首先,这是我的位置模型。它有一个被称为成员的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
位置
不是位置
对象,而是包含一个位置
对象的querySet
。但是,不需要提取此问题,您可以使用:但是实际上,您无需将
成员
传递给上下文:在模板中,您可以使用:Your
location
is not aLocation
object, but aQuerySet
that contains oneLocation
object. Fetching this however is not necessary, you can work with:But in fact you do not need to pass the
members
to the context: in the template you can work with:我发现误会,
不是
I found the misake, it's
and not