【问题标题】:how to find intersection of django models?如何找到 Django 模型的交集?
【发布时间】:2017-05-03 11:38:22
【问题描述】:

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

现在,我想要实现的是获取属于用户类型的大学交叉点的所有用户。 假设我选择了 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'

那么我怎样才能找到交叉点....如果您发现任何错误,请原谅我。 提前谢谢

【问题讨论】:

    标签: python django django-models


    【解决方案1】:

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

    那么你能做的就像

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

    【讨论】:

    • 当用户告诉我员工类型和大学名称时,我想要属于大学和特定员工类型的所有用户
    • 是的,上面的查询将帮助您实现您想要实现的目标。检查并让我知道结果是否不适合您。干杯
    • 那行得通,我使用了大学和用户类型的多个值,所以我使用了 usertype_id__in.thanks
    • 太棒了。很高兴帮助你:)
    猜你喜欢
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2010-09-25
    • 1970-01-01
    • 2011-04-11
    相关资源
    最近更新 更多