简体   繁体   English

Django - 如何在模板中获取长度和计数命令? (模板中的查询集)

[英]Django - how to get length and count commands in template? (query-set in template)

I have these modules: Sales, Gender and Type in Django.我有这些模块:Django 中的 Sales、Gender 和 Type。

Table 1:表格1:

class Sales(models.Model):
    sname = models.CharField(max_length=100, default="-")
    sgender = models.ForeignKey(MainGender, on_delete=models.CASCADE)
    stype = models.ForeignKey(MainTypes, on_delete=models.CASCADE)
    sdate = models.TextField(default="-")   

    +------+-----+------+------+
    | name |gende| type | date |
    +------+-----+------+------+
    | A    | 1   | 1    | 2019 |
    +------+-----+------+------+
    | B    | 2   | 1    | 2018 |
    +------+-----+------+------+
    | A    | 1   | 3    | 2019 |
    +------+-----+------+------+
    | C    | 2   | 3    | 2017 |
    +------+-----+------+------+
    | A    | 1   | 2    | 2019 |
    +------+-----+------+------+

Table 2 (key)表2(重点)

class MainGender(models.Model):
    mid = models.CharField(max_length=25, default="-")
    mgender = models.CharField(max_length=25, default="-")

+----+--------+
| id | gender |
+----+--------+
| 1  | Male   |
+----+--------+
| 2  | Female |
+----+--------+
| 3  | -      |
+----+--------+

Table 3 (key)表 3(重点)

class MainTypes(models.Model):
    tid = models.CharField(max_length=50, default="-")
    ttype = models.CharField(max_length=50, default="-")

+----+------+
| id | type |
+----+------+
| 1  | U1   |
+----+------+
| 2  | X2   |
+----+------+
| 3  | B1   |
+----+------+
| 4  | H3   |
+----+------+

Then, I want to count all the things in the main table and show them in the template as below然后,我想计算主表中的所有内容并将它们显示在模板中,如下所示

+---------------+---+
| Total records | 5 |
+---------------+---+
| Male          | 3 |
+---------------+---+
| Female        | 2 |
+---------------+---+
| Type U1       | 2 |
+---------------+---+
| Type X2       | 1 |
+---------------+---+
| Type B1       | 2 |
+---------------+---+
| Type H3       | 0 |
+---------------+---+

I can only get total records in template by code below我只能通过下面的代码获取模板中的总记录

in views.py在views.py中

def Sale_p(request):
    saless = Sale.objects.all()
    return render(request, 'sale.html', {'sales':saless})

I tried these codes in the template (sale.html)我在模板 (sale.html) 中尝试了这些代码

{{ sales|length }} (gives you total records in Table 1, in this example 5)

{{ sales.gender|length }} (gives nothing)

{{ sales.type|length }} (gives nothing) 

{{ sales.gender_set|length }} (gives nothing)

{{ sales.type_set|length }} (gives nothing)

{{ sales.gender(1)|length }} (gives nothing)

{{ sales(gender='1')|length }} (gives nothing)

{{ sales.gender.count() }} (gives nothing)

They did not work in template.他们没有在模板中工作。 I want to see in records how many times we had 'male' or 'female' or types and so on...我想在记录中看到我们有多少次“男性”或“女性”或类型等等......

in template, sale.html在模板中,sale.html

+---------------+--------------------+
| Total records | {{ sales|length }} |
+---------------+--------------------+
| Male          | ?                  |
+---------------+--------------------+
| Female        | ?                  |
+---------------+--------------------+
| Type U1       | ?                  |
+---------------+--------------------+
| Type X2       | ?                  |
+---------------+--------------------+
| Type B1       | ?                  |
+---------------+--------------------+
| Type H3       | ?                  |
+---------------+--------------------+

What should be the codes to get the results for others?为他人获取结果的代码应该是什么? Or should I use different ways in views.py?或者我应该在 views.py 中使用不同的方式?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM