'CategoryDetailView' 对象没有属性 'get_object'
'CategoryDetailView' object has no attribute 'get_object'
我正在为我的 CRM 设置我的 CategoryDetailView。然后出现这个错误:
'CategoryDetailView'对象没有属性'get_object'
这是我的代码部分来自 views.py:
class CategoryDetailView(LoginRequiredMixin, generic.ListView):
template_name = "clients/category/category_detail.html"
context_object_name = "category"
def get_context_data(self, **kwargs):
context = super(CategoryDetailView, self).get_context_data(**kwargs)
clients = self.get_object().client_set.all()
context.update({
"clients": clients
})
return context
这是我的 models.py
class Client(models.Model):
first_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
mobile_number = models.CharField(max_length=12)
organization = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
agent = models.ForeignKey("Agent", null=True, blank=True, on_delete=models.SET_NULL)
category = models.ForeignKey("Category", related_name="clients", null=True, blank=True, on_delete=models.SET_NULL)
class Category(models.Model):
name = models.CharField(max_length=30) # New, Tapped, Active, Closed
organization = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
def __str__(self):
return self.name
提前致谢!
您正在使用ListView
[Django-doc], not a DetailView
[Django-doc], and a ListView
indeed has no .get_object(…)
method [Django-doc]。您还应该指定 model = …
或 queryset = …
来指定 DetailView
正在处理的查询集。
因此您应该继承 DetailView
。您还可以使用 self.object
来防止进行额外的查询:
from django.views.generic import <b>DetailView</b>
class CategoryDetailView(LoginRequiredMixin, <b>DetailView</b>):
template_name = 'clients/category/category_detail.html'
context_object_name = 'category'
<b>model = Category</b>
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
clients = <b>self.object.</b>client_set.all()
context['clients'] = clients
return context
也没有理由将其添加到上下文中,在模板中,您可以简单地使用:
{% for client in category<b>.client_set.all</b> %}
{{ client }}
{% endfor %}
我正在为我的 CRM 设置我的 CategoryDetailView。然后出现这个错误:
'CategoryDetailView'对象没有属性'get_object'
这是我的代码部分来自 views.py:
class CategoryDetailView(LoginRequiredMixin, generic.ListView):
template_name = "clients/category/category_detail.html"
context_object_name = "category"
def get_context_data(self, **kwargs):
context = super(CategoryDetailView, self).get_context_data(**kwargs)
clients = self.get_object().client_set.all()
context.update({
"clients": clients
})
return context
这是我的 models.py
class Client(models.Model):
first_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
mobile_number = models.CharField(max_length=12)
organization = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
agent = models.ForeignKey("Agent", null=True, blank=True, on_delete=models.SET_NULL)
category = models.ForeignKey("Category", related_name="clients", null=True, blank=True, on_delete=models.SET_NULL)
class Category(models.Model):
name = models.CharField(max_length=30) # New, Tapped, Active, Closed
organization = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
def __str__(self):
return self.name
提前致谢!
您正在使用ListView
[Django-doc], not a DetailView
[Django-doc], and a ListView
indeed has no .get_object(…)
method [Django-doc]。您还应该指定 model = …
或 queryset = …
来指定 DetailView
正在处理的查询集。
因此您应该继承 DetailView
。您还可以使用 self.object
来防止进行额外的查询:
from django.views.generic import <b>DetailView</b>
class CategoryDetailView(LoginRequiredMixin, <b>DetailView</b>):
template_name = 'clients/category/category_detail.html'
context_object_name = 'category'
<b>model = Category</b>
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
clients = <b>self.object.</b>client_set.all()
context['clients'] = clients
return context
也没有理由将其添加到上下文中,在模板中,您可以简单地使用:
{% for client in category<b>.client_set.all</b> %}
{{ client }}
{% endfor %}