- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我的大杂烩应用程序的查询超出了我对 Django 的 ORM 工作原理的了解。
这是我当前的(不正确的)尝试:
queryset = Mentor.objects.filter(
shift__session = session,
jobs_desired = job
).exclude(
shift__session = session,
shift__jobs__time = job.time
)
如果你想通读它们,我的模型在下面。
最初的 filter()
工作正常。我的问题是链接到末尾的 exclude()
。
exclude()
似乎在排除 Mentor
:
Shift
(shift__session = session
),Shift
满足第二组标准 shift__jobs__time = job.time
。我只想过滤掉 Mentor
的 Shift
与之关联,同时满足两个条件。
有什么想法吗?
class DojoSession(models.Model):
term = models.ForeignKey(DojoTerm, help_text = "Dojo Term")
date = models.DateField(
blank = False,
help_text = "Date during which the session will take place."
)
start = models.TimeField(
blank = False,
help_text = "Start Time"
)
end = models.TimeField(
blank = False,
help_text = "End Time"
)
rooms = models.ManyToManyField(
Room,
blank = True,
help_text = "The rooms in which this session will be running."
)
class Shift(models.Model):
mentor = models.ForeignKey(
'mentors.Mentor',
blank = False,
help_text = 'The mentor unergoing this shift.'
)
session = models.ForeignKey(
DojoSession,
blank = False,
help_text = 'The session during which this shift takes place.',
)
role = models.ForeignKey(
'mentors.Role',
blank = False,
help_text = "The role that the mentor will be undertaking during this shift.",
)
room = models.ForeignKey(
Room,
blank = True,
null = True,
help_text = "The room, if any, that the mentor will be undertaking the shift in."
)
jobs = models.ManyToManyField(
'jobs.Job',
blank = True,
null = True,
)
start = models.TimeField(
blank = False,
help_text = "Start Time"
)
end = models.TimeField(
blank = False,
help_text = "End Time"
)
class Job(models.Model):
BEFORE = 'B'
DURING = 'D'
AFTER = 'A'
TIME_CHOICES = (
(BEFORE, 'Before session'),
(DURING, 'During session'),
(AFTER, 'After session'),
)
name = models.CharField(
max_length = 50,
help_text = "The job's name."
)
description = models.TextField(
max_length = 1024,
help_text = "A description of the job."
)
location = models.CharField(
max_length = 50,
help_text = "The job's location."
)
time = models.CharField(
max_length = 1,
choices = TIME_CHOICES,
help_text = "The time during a session at which this job can be carried out."
)
class Mentor(models.Model):
MALE_SMALL = "MS"
MALE_MEDIUM = "MM"
MALE_LARGE = "ML"
MALE_EXTRA_LARGE = "MXL"
FEMALE_EXTRA_SMALL = "FXS"
FEMALE_SMALL = "FS"
FEMALE_MEDIUM = "FM"
FEMALE_LARGE = "FL"
FEMALE_EXTRA_LARGE = "FXL"
SHIRT_SIZE_CHOICES = (
('Male', (
(MALE_SMALL, "Male S"),
(MALE_MEDIUM, "Male M"),
(MALE_LARGE, "Male L"),
(MALE_EXTRA_LARGE, "Male XL")
)),
('Female', (
(FEMALE_EXTRA_SMALL, "Female XS"),
(FEMALE_SMALL, "Female S"),
(FEMALE_MEDIUM, "Female M"),
(FEMALE_LARGE, "Female L"),
(FEMALE_EXTRA_LARGE, "Female XL")
))
)
ASSOCIATE = 'A'
STAFF = 'S'
NEITHER = 'N'
CURTIN_STATUS_CHOICES = (
(ASSOCIATE, 'Associate'),
(STAFF, 'Staff'),
(NEITHER, 'Neither/not sure')
)
NOTHING = 'NO'
SOMETHING = 'SO'
EVERYTHING = 'EV'
KNOWLEDGE_CHOICES = (
(NOTHING, 'I know nothing but am keen to learn!'),
(SOMETHING, 'I know some basics'),
(EVERYTHING, 'I know a great deal')
)
uni = models.CharField(
max_length = 50,
null = True,
blank = True,
help_text = "University of study"
)
uni_study = models.CharField(
max_length = 256,
null = True,
blank = True,
help_text = "If you're attending university, what are you studying?"
)
work = models.CharField(
max_length = 256,
null = True,
blank = True,
help_text = "If you workwhat do you do?"
)
shirt_size = models.CharField(
max_length = 3,
blank = True,
choices = SHIRT_SIZE_CHOICES,
help_text = "T-shirt size (for uniform)"
)
needs_shirt = models.BooleanField(
default = True,
help_text = "Does the mentor need to have a shirt provisioned for them?"
)
wwcc = models.CharField(
max_length = 10,
verbose_name = "WWCC card number",
blank = True,
null = True,
help_text = "WWCC card number (if WWCC card holder)"
)
wwcc_receipt = models.CharField(
max_length = 15,
verbose_name = "WWCC receipt number",
blank = True,
null = True,
help_text = "WWCC receipt number (if WWCC is processing)"
)
curtin_status = models.CharField(
max_length = 1,
verbose_name = "Current Curtin HR status",
choices = CURTIN_STATUS_CHOICES,
default = NEITHER,
blank = False,
help_text = "When possible, we recommend that all CoderDojo mentors are either Curtin University Associates or Staff members."
)
curtin_id = models.CharField(
max_length = 10,
verbose_name = "Curtin Staff/Associate ID",
blank = True,
null = True,
help_text = "Your Curtin Staff/Associate ID (if applicable)"
)
coding_experience = models.CharField(
max_length = 2,
blank = False,
default = NOTHING,
choices = KNOWLEDGE_CHOICES,
help_text = "How much programming experience do you have?"
)
children_experience = models.CharField(
max_length = 2,
blank = False,
default = NOTHING,
choices = KNOWLEDGE_CHOICES,
help_text = "How much experience do you have with children?"
)
roles_desired = models.ManyToManyField(Role)
jobs_desired = models.ManyToManyField('jobs.Job')
shift_availabilities = models.ManyToManyField(
'planner.DojoSession',
help_text = "When are you available?"
)
user = models.OneToOneField(settings.AUTH_USER_MODEL,
unique = True
)
最佳答案
首先,让我们解释一下这里发生了什么。当你写的时候:
set.exclude( A=arg1, B=arg2 )
这转化为以下查询:
SELECT [...] WHERE NOT (A=arg1 AND B=arg2)
在boolean algebra , ¬(A ∧ B) (not[A and B]) 实际上是 (¬A ∨ ¬B) (not[A] OR not[B])。因此,您在查询中的意思是:
SELECT [...] WHERE NOT(A=arg1) OR NOT(B=arg2)
在编写具有多个参数的exclude
过滤器时,请牢记这一点。
因此,如果在您的查询中,您想要排除同时检查两个标准的元素(如果您愿意,则为标准的交集),最简单和最好的方法是链排除过滤器 :
set.exclude(A=arg1).exclude(B=arg2)
查询集操作是惰性,大致意味着您的exclude
过滤器将同时被评估。所以两个过滤器不会“加倍工作”。
过滤器将转化为:
SELECT [...] WHERE NOT(A=arg1) AND NOT(B=arg2)
这正是您想要的!
编写查询有时会很困难,但请记住:
exclude
过滤器即可。现在,这是您的新查询:
queryset = Mentor.objects.filter(
shift__session = session,
jobs_desired = job
).exclude(
shift__session = session
).exclude(
shift__jobs__time = job.time
)
如果我们“扁平化”你的要求,你想要:
filter(shift__session = session)
.exclude(shift__session = session)
生成的 SQL 将是:
SELECT [...] WHERE shift__session = session AND [...] AND NOT(shift__session = session)
但是 A ∧ ¬A (A AND NOT[A]) 是空集。所以问题出在您查询的语义上。
从你的帖子我读到:
excluding [...] an associated Shift which meets the conditions specified (shift__session = session) AND a (possibly different) associated Shift that meets the second set of criteria
您使用的 filter
已经保证 shift__session = session
,所以您不应该将它放在 exclude
过滤器中。
根据我的猜测(如果我错了请告诉我),你想要的是:
queryset = Mentor.objects.filter(
shift__session = session,
jobs_desired = job
).exclude(
shift__jobs__time = job.time
)
关于python - 复杂的 Django 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25136445/
我有三张 table 。表 A 有选项名称(即颜色、尺寸)。表 B 有选项值名称(即蓝色、红色、黑色等)。表C通过将选项名称id和选项名称值id放在一起来建立关系。 我的查询需要显示值和选项的名称,而
在mysql中,如何计算一行中的非空单元格?我只想计算某些列之间的单元格,比如第 3-10 列之间的单元格。不是所有的列...同样,仅在该行中。 最佳答案 如果你想这样做,只能在 sql 中使用名称而
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
我正在为版本7.6进行Elasticsearch查询 我的查询是这样的: { "query": { "bool": { "should": [ {
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 7 年前。 Improve this ques
是否可以编写一个查询来检查任一子查询(而不是一个子查询)是否正确? SELECT * FROM employees e WHERE NOT EXISTS (
我找到了很多关于我的问题的答案,但问题没有解决 我有表格,有数据,例如: Data 1 Data 2 Data 3
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
以下查询返回错误: 查询: SELECT Id, FirstName, LastName, OwnerId, PersonEmail FROM Account WHERE lower(PersonEm
我从 EditText 中获取了 String 值。以及提交查询的按钮。 String sql=editQuery.getText().toString();// SELECT * FROM empl
我有一个或多或少有效的查询(关于结果),但处理大约需要 45 秒。这对于在 GUI 中呈现数据来说肯定太长了。 所以我的需求是找到一个更快/更高效的查询(几毫秒左右会很好)我的数据表大约有 3000
这是我第一次使用 Stack Overflow,所以我希望我以正确的方式提出这个问题。 我有 2 个 SQL 查询,我正在尝试比较和识别缺失值,尽管我无法将 NULL 字段添加到第二个查询中以识别缺失
什么是动态 SQL 查询?何时需要使用动态 SQL 查询?我使用的是 SQL Server 2005。 最佳答案 这里有几篇文章: Introduction to Dynamic SQL Dynami
include "mysql.php"; $query= "SELECT ID,name,displayname,established,summary,searchlink,im
我有一个查询要“转换”为 mysql。这是查询: select top 5 * from (select id, firstName, lastName, sum(fileSize) as To
通过我的研究,我发现至少从 EF 4.1 开始,EF 查询上的 .ToString() 方法将返回要运行的 SQL。事实上,这对我来说非常有用,使用 Entity Framework 5 和 6。 但
我在构造查询来执行以下操作时遇到问题: 按activity_type_id过滤联系人,仅显示最近事件具有所需activity_type_id或为NULL(无事件)的联系人 表格结构如下: 一个联系人可
如何让我输入数据库的信息在输入数据 5 分钟后自行更新? 假设我有一张 table : +--+--+-----+ |id|ip|count| +--+--+-----+ |
我正在尝试搜索正好是 4 位数字的 ID,我知道我需要使用 LENGTH() 字符串函数,但找不到如何使用它的示例。我正在尝试以下(和其他变体)但它们不起作用。 SELECT max(car_id)
我有一个在 mysql 上运行良好的 sql 查询(查询 + 连接): select sum(pa.price) from user u , purchase pu , pack pa where (
我是一名优秀的程序员,十分优秀!