2017-05-03 69 views
1

我有三種模式的大學,用戶類型和用戶。大學模式包含所有大學和用戶類型列表包含兩種類型的教師和學生,用戶模型包含所有用戶。如何找到django模型的交集?

現在,我想實現的是獲取屬於用戶類型的大學路口的所有用戶。 假設我選擇了abc大學和用戶類型faculty,那麼我怎樣才能從該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' 

so ho我能找到交點......如果你發現任何錯誤,請原諒我。 Thansk提前

回答

0

將用戶輸入員工類型和大學名稱?

那麼你可以做的是像

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

當用戶告訴我的員工塔伊和大學的名字的話,我希望所有的用戶屬於大學和具體工作人員類型 –

+0

是上面的查詢會幫你與你正在努力實現的。 檢查並讓我知道如果結果不適合你。 乾杯 – Exprator

+0

工作我是使用多種價值的大學和usertype,所以我用usertype_id__in.thanks –