如何找到django模型的交集?

时间:2017-05-03 11:38:22

标签: python django django-models

我有三个模型大学,用户类型和用户。大学模型包含所有大学的列表,用户类型包含两种类型的用户类型教师和学生,用户模型包含所有用户。

现在,我想要实现的是获取属于用户类型的大学交叉点的所有用户。 假设我选择了abc大学和用户类型的教师,那么我如何从教师类型的abc大学获得所有用户。

我的模特可以帮助你更好地理解: -

大学模式: -

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

# 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.ForeignKey(User,db_column="updatedBy",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.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'

和用户模型: -

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="")
    graduationYear = models.IntegerField(db_column='graduationYear',max_length=11,help_text="")  
    canAddNews = models.BooleanField(db_column='canAddNews',default=False,help_text="")  
    receivePrivateMsgNotification = models.BooleanField(db_column='receivePrivateMsgNotification',default=True ,help_text="")  
    receivePrivateMsg = models.BooleanField(db_column='receivePrivateMsg',default=True ,help_text="")
    receiveCommentNotification = models.BooleanField(db_column='receiveCommentNotification',default=True ,help_text="")  
    receiveLikeNotification = models.BooleanField(db_column='receiveLikeNotification',default=True ,help_text="")  
    receiveFavoriteFollowNotification = models.BooleanField(db_column='receiveFavoriteFollowNotification',default=True ,help_text="")  
    receiveNewPostNotification = models.BooleanField(db_column='receiveNewPostNotification',default=True ,help_text="")  
    allowInPopularList = models.BooleanField(db_column='allowInPopularList',default=True ,help_text="")  
    xmppResponse = models.TextField(db_column='xmppResponse',help_text="")  
    xmppDatetime = models.DateTimeField(db_column='xmppDatetime', 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 ......")
    lastPasswordReset = models.DateTimeField(db_column='lastPasswordReset',help_text="")
    authorities = models.CharField(db_column="departmentId",max_length=255,help_text="")

    class Meta:
        managed = False
        db_table = 'users'

所以我怎么能找到这个交叉点......如果你发现任何错误,请原谅我。 Thansk提前

1 个答案:

答案 0 :(得分:0)

用户会输入员工类型和大学名称吗?

然后你可以做的就像

User.objects.filter(usertype_id=inputted_id, university_id=inputted_id)