Python Django-来自.values和.annotate的意外结果
我试图统计表中不同产品名称的出现次数。我希望结果是这样的:Python Django-来自.values和.annotate的意外结果,python,django,django-models,django-templates,Python,Django,Django Models,Django Templates,我试图统计表中不同产品名称的出现次数。我希望结果是这样的: Laptop 10 Mice 15 依此类推,但我得到的却是: 2 10 1 15 有什么想法吗 这是我的型号。py: class ProductNoSerial(models.Model): name = models.CharField(max_length=100) category = models.ForeignKey(Category) def __str__(self): re
Laptop 10
Mice 15
依此类推,但我得到的却是:
2 10
1 15
有什么想法吗
这是我的型号。py:
class ProductNoSerial(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category)
def __str__(self):
return self.name
class ProductNoSerialInstance(models.Model):
STATUS_CHOICES = (
('in_inventory', 'In Stock'),
('given_out', 'Given Out'),
('repair', 'Repair')
)
name = models.ForeignKey(ProductNoSerial)
owner = models.ForeignKey(Employee, blank=True, null=True)
it_dep = models.ForeignKey(ItDep)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft')
def __unicode__(self):
return self.name
def InventoryList(request):
count_query = ProductNoSerialInstance.objects.values('name').annotate(count=models.Count('name'))
context = { 'inventory_list': count_query }
return render(request, 'inventory_list.html', context)
<p><b>List of inventory<b></p>
<table>
{% for item in inventory_list %}
<tr>
<td> {{ item.name }} </td>
<td> {{ item.count }} </td>
</tr>
{% endfor %}
</table>
这是我的视图。py:
class ProductNoSerial(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category)
def __str__(self):
return self.name
class ProductNoSerialInstance(models.Model):
STATUS_CHOICES = (
('in_inventory', 'In Stock'),
('given_out', 'Given Out'),
('repair', 'Repair')
)
name = models.ForeignKey(ProductNoSerial)
owner = models.ForeignKey(Employee, blank=True, null=True)
it_dep = models.ForeignKey(ItDep)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft')
def __unicode__(self):
return self.name
def InventoryList(request):
count_query = ProductNoSerialInstance.objects.values('name').annotate(count=models.Count('name'))
context = { 'inventory_list': count_query }
return render(request, 'inventory_list.html', context)
<p><b>List of inventory<b></p>
<table>
{% for item in inventory_list %}
<tr>
<td> {{ item.name }} </td>
<td> {{ item.count }} </td>
</tr>
{% endfor %}
</table>
最后是我的库存清单.html:
class ProductNoSerial(models.Model):
name = models.CharField(max_length=100)
category = models.ForeignKey(Category)
def __str__(self):
return self.name
class ProductNoSerialInstance(models.Model):
STATUS_CHOICES = (
('in_inventory', 'In Stock'),
('given_out', 'Given Out'),
('repair', 'Repair')
)
name = models.ForeignKey(ProductNoSerial)
owner = models.ForeignKey(Employee, blank=True, null=True)
it_dep = models.ForeignKey(ItDep)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='draft')
def __unicode__(self):
return self.name
def InventoryList(request):
count_query = ProductNoSerialInstance.objects.values('name').annotate(count=models.Count('name'))
context = { 'inventory_list': count_query }
return render(request, 'inventory_list.html', context)
<p><b>List of inventory<b></p>
<table>
{% for item in inventory_list %}
<tr>
<td> {{ item.name }} </td>
<td> {{ item.count }} </td>
</tr>
{% endfor %}
</table>
库存清单
{存货清单%中项目的百分比}
{{item.name}
{{item.count}
{%endfor%}
我试过了,但运气不好。你知道哪里不对吗?我认为问题可能是因为
name
是一个外键?这里有一个微妙之处,乍一看不是很明显,因为ProductNoSerialInstance
模型有一个名为name
的字段,实际上是一个外键。再加上你得到的是值
,这就是结果<代码>值返回字典而不是对象实例,因此{{{item.name.name}}
很不幸无法正常工作。你需要对你的查询稍作修改
ProductNoSerialInstance.objects.values('name__name').annotate(count=models.Count('name'))
将
{{item.name}}
替换为{{{item.name.name}}
@albar,如果您认为这是问一个正确的答案。把它写在答案上。@albar嗨,albar,谢谢你的建议。第一列现在已经消失了,产生了1015
您查询了错误的内容。您应该在ProductNoSerial上运行注释,并计算相关ProductNoSerialInstances的数量。您好,e4c6,这可以正常工作!当然,我还必须在库存清单.html
中将{{item.name}}
更改为{{item.name}
。非常感谢你!很高兴能帮上忙。