Issue
I am trying to show the number of each category in Leads. But i am unable to show it. Appriciate your help.
I am learning django and python. Tried many opiton, but failed.
Below is my code.
Model:
class Leads(models.Model):
first_name = models.CharField(max_length=20)
last_name = models.CharField(max_length=20)
age = models.IntegerField(default=0)
orginasation = 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="leads", on_delete= models.SET_NULL, null=True, blank=True)
def __str__(self):
return f"{self.first_name} {self.last_name}"
class Category(models.Model):
name = models.CharField(max_length=30,) # New, Contact, Converted, Unconverted
orginasation = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
def __str__(self):
return self.name
View:
class CategoryListView(LoginRequiredMixin, generic.ListView):
template_name = "leads/category_list.html"
context_object_name = "category_list"
def get_queryset(self, **kwargs):
user = self.request.user
#initial query set for entire organisation
if user.is_organisor:
queryset = models.Category.objects.filter(orginasation=user.userprofile)
else:
queryset = models.Category.objects.filter(orginasation=user.agent.orginasation)
# count = models.Leads.objects.filter(category = 3).count()
return queryset
template
{% for category in category_list %}
<tr>
<td>{{category.name}}/td>
<td>{{category.count}}</td>
</tr>
{% endfor %}
Solution
If I understand your issue, you should try change from {{category.count}}
to {{ category.leads.all.count }}
in your template.
Answered By - NixonSparrow
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.