反向查找Django外键?

时间:2017-05-29 05:37:13

标签: python django

我有三个模型大学,用户和主用户类型。模型定义如下。

大学模式: -

from __future__ import unicode_literals
from django.db import models


# WE ARE AT MODELS/UNIVERSITIES

class Universities(models.Model):
    id = models.IntegerField(db_column="id", max_length=11, help_text="")
    name = models.CharField(db_column="name", max_length=255, help_text="")
    abbreviation = models.CharField(db_column="abbreviation", max_length=255, help_text="")
    address = models.CharField(db_column="address", max_length=255, help_text="")
    status = models.BooleanField(db_column="status", default=False, help_text="")
    createdAt = models.DateTimeField(db_column='createdAt', auto_now=True, help_text="")  
    modifiedAt = models.DateTimeField(db_column='modifiedAt', auto_now=True, help_text="")  
    updatedBy = models.IntegerField(db_column="updatedBy",default=None,help_text="Logged in user updated by ......")

    class Meta:
        managed = False
        get_latest_by = 'createdAt'
        db_table = 'universities'

用户模型:

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="")
    mobileNo = models.CharField(db_column='mobileNo', max_length=16,help_text="")  
    dob = models.DateField(db_column="dob",help_text="")
    major = models.CharField(db_column="major",max_length=255,help_text="")
         class Meta:
        managed = False
        db_table = 'users'

和用户类型模型: -

from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.db import models

# WE ARE AT MODELS/MASTER USERS TYPES

class MasterUserTypes(models.Model):
    id = models.IntegerField(db_column="id", max_length=11, help_text="")
    userType = models.CharField(db_column='userType', max_length=255, help_text="")
    description = models.CharField(db_column='desciption', max_length=255, help_text="")
    status = models.BooleanField(db_column="status", default=False, help_text="")
    createdAt = models.DateTimeField(db_column='createdAt', auto_now=True, help_text="")  
    modifiedAt = models.DateTimeField(db_column='modifiedAt', auto_now=True, help_text="")  
    updatedBy = models.ForeignKey(User, db_column='updatedBy',
                                  help_text="Logged in user updated by ......")

    class Meta:
        managed = False
        db_table = 'master_user_types'

在我的情况下,有两种类型的用户,一种是学生,另一种是教师。在MasterUserTypes模型中定义了tyoe。我通过使用revser查找在大学中找到了总用户我正在获得用户大学明智的这样: -

{{ item.users_set.all.count }}

其中item是一个大学对象,从视图传递为contex.This是通过反向延迟提取完成的,但是当我为用户类型应用conditon时它不起作用: -

{% if item.users_set.model.masterusertypes.id == 1  %} {{ item.users_set.all.count  }} {% else %} {{ 0 }} {% endif %}

我想使用反向懒惰来找出有多少学生。

0 个答案:

没有答案