我很想知道我们可以在Django模板中使用过滤器标签,就像我们在视图中一样。
就像我们在观点中一样: -
studentcount = Users.objects.using('cms').filter(universityId=item.id,userTypeId=2).count()
我们可以在模板中使用类似的东西: -
<td>{{ item.users.userTypeId=1_set.all.count }}</td>
我知道上面的方法是错误的,并且没有工作只是写它以便更好地理解。我的用户模型是存在的,以防你想看到我的用户模型。
我有一个用户表,有3种类型的用户学生,教师和俱乐部。
我的用户模型: -
from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from cms.models.masterUserTypes import MasterUserTypes
from cms.models.universities import Universities
from cms.models.departments import MasterDepartments
# WE ARE AT MODELS/APPUSERS
requestChoice = (
('male', 'male'),
('female', 'female'),
)
class Users(models.Model):
id = models.IntegerField(db_column="id", max_length=11, help_text="")
userTypeId = models.ForeignKey(MasterUserTypes, db_column="userTypeId")
universityId = models.ForeignKey(Universities, db_column="universityId")
departmentId = models.ForeignKey(MasterDepartments , db_column="departmentId",help_text="")
name = models.CharField(db_column="name",max_length=255,help_text="")
username = models.CharField(db_column="username",unique=True, max_length=255,help_text="")
email = models.CharField(db_column="email",unique=True, max_length=255,help_text="")
password = models.CharField(db_column="password",max_length=255,help_text="")
bio = models.TextField(db_column="bio",max_length=500,help_text="")
gender = models.CharField(db_column="gender",max_length=6, choices=requestChoice,help_text="")
class Meta:
managed = False
db_table = 'users'