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

时间:2020-02-25 11:02:14

标签: django django-views django-templates

我有以下模块:Django中的Sales,性别和Type。

表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 |
    +------+-----+------+------+

表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  | -      |
+----+--------+

表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   |
+----+------+

然后,我要计算主表中的所有内容,并在下面的模板中显示它们

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

我只能通过下面的代码获取模板中的总记录

in views.py

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

我在模板(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)

它们在模板中不起作用。我想在记录中查看我们有“男性”或“女性”或类型等等的次数...

在sale.html模板中

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

要为他人获得结果的代码应该是什么? 还是应该在views.py中使用其他方式?

1 个答案:

答案 0 :(得分:0)