django在聚合后获取基础queryset项
对于这些型号:django在聚合后获取基础queryset项,django,Django,对于这些型号: class Product(models.Model): code = models.IntegerField() class Failures(models.Model): FAILURE_CHOICES = ( ('SC', 'Screen'), ('BA', 'Battery'), ('BX', 'Box'), # etc, with 10+ choices ) name = models.CharFie
class Product(models.Model):
code = models.IntegerField()
class Failures(models.Model):
FAILURE_CHOICES = (
('SC', 'Screen'),
('BA', 'Battery'),
('BX', 'Box'),
# etc, with 10+ choices
)
name = models.CharField(max_length=2, choices=FAILURE_CHOICES)
class Market(models.Model):
MARKET_CHOICES = (
('ED', 'Education'),
('CO', 'Commercial'),
# etc, with 10 choices
)
product = models.ForeignKey(Product)
market = models.CharField(max_length=2,choices=MARKET_CHOICES)
failures = models.ManyToManyField(Failures, blank=True)
我有这样一个观点:
fails = ['SC','BA','BX',etc]
markets = [['ED'],['CO'],etc]
def fail_counter(fails):
dictlist=[]
for failure in fails:
# dict1 = {failure:count}
c = Failures(name=failure)
d = str(c.get_name_display())
kwargs = {d: (Count('failures'))}
dict1 = total.filter(market__in=mark, failures__name=failure).aggregate(**kwargs)
dictlist.append(dict1)
return(dictlist)
fail_list=[]
for mark in markets:
fail_list.append(fail_counter(fails))
然后将fail_列表传递给模板,模板将其解压到表中,
所以我得到了乙二醇
Market1(education) Market2(commercial) Market3(etc)
Failure Count Failure Count Failure Count
Screen 0 Screen 0 Screen 0
Battery 0 Battery 1 Battery 0
Box 1 Box 0 Box 0
etc
将任何计数>0转换为指向管理员之类的链接的最佳方法是什么
该计数中特定产品代码的更改列表?在模板中,您可以使用密码,是否尝试测试
if value>0
。或者你到底需要什么?如果计数大于0,我需要得到实际的查询集。目前,我只是将计数发送到此模板。我可以将queryset和count都放在上下文中(似乎太多了),但是如何将queryset从这个模板发送到另一个模板(如果count>0)?