Django查询优化:根据多对一到多对多查找对象列表
I have an ugly loop that is operating as a band-aid in Django that I'd like to optimize into a join. I've broken it down to the most basic structure to help simplify the problem. I'm hoping someone else has run into something like this before -- if not, I'll share the answer when I finally fix it.
我有一个丑陋的循环,作为Django的创可贴,我想优化成一个连接。我把它分解为最基本的结构,以帮助简化问题。我希望其他人之前碰到过这样的事情 - 如果没有,我会在最后修复时分享答案。
To summarize
- Object A has a non-symmetrical, many-to-many relationship with itself.
- Object B has a many-to-one relationship with Object A.
- Given an ObjectB, I need a set of other ObjectBs that are the children of the ObjectAs that are associated with our ObjectB's parent ObjectA.
对象A与其自身具有非对称的多对多关系。
对象B与对象A具有多对一关系。
给定一个ObjectB,我需要一组其他ObjectB,它们是与ObjectB的父ObjectA相关联的ObjectAs的子代。
I'm sure someone with more database experience could phrase that better (leave a comment if you have a better description of this association).
我确信拥有更多数据库经验的人可能会更好地说出来(如果您对此关联有更好的描述,请留言)。
Here is a bare bones example of the structure I'm working with in Django, and the sort of loop that's running:
这是我在Django中使用的结构的一个简单的例子,以及正在运行的循环类型:
class ObjectA(models.Model):
object_a_rules = models.ManyToManyField("self", symmetrical=False, through='ObjectARule')
class ObjectARule(models.Model):
object_a_one = models.ForeignKey(ObjectA, related_name="object_a_before")
object_a_two = models.ForeignKey(ObjectA, related_name="object_a_after")
class ObjectB(models.Model):
object_a_association = models.ForeignKey(ObjectA)
def that_annoying_loop_i_mentioned(self):
object_a_rules_list = ObjectARule.objects.filter(object_a_one = self.object_a_association)
#A list of all of the ObjectARules that have the ObjectA this ObjectB is associated with
#as the first half of the many-to-many relationship.
object_b_list = ObjectB.objects.all()
#A list of all of the ObjectBs, may also be a filtered list
for object_a_rule in object_a_rules_list:
for object_b in object_b_list:
if (object_a_rule.object_a_two == object_b.object_a_association):
#if the second half of ObjectARule is the ObjectA of
#the ObjectB in this list, then do something with that ObjectB.
pass
How could Django get a list of ObjectBs through a join, so this painfully inefficient loop wouldn't have to run?
Django如何通过连接获得ObjectB的列表,所以这个非常低效的循环不必运行?
1 个解决方案
#1
2
Given an ObjectB, I need a set of other ObjectBs that are the children of the ObjectAs that are associated with our ObjectB's parent ObjectA.
给定一个ObjectB,我需要一组其他ObjectB,它们是与ObjectB的父ObjectA相关联的ObjectAs的子代。
If objb
is the ObjectB you are given, you could do this as follows:
如果objb是您给出的ObjectB,您可以按如下方式执行此操作:
objects = ObjectB.objects.filter(object_a_association__object_a_rules=objb.object_a_association)
or alternatively,
objects = ObjectB.objects.filter(object_a_association__object_a_rules__objectb_set=objb)
See also Lookups that span relationships
另请参阅跨越关系的查找
更多相关文章
- PostgreSQL操纵大对象(图片等)
- Recordset记录集对象的属性
- GenericObjectPool对象池异常排查
- Java对象创建的过程及对象的内存布局与访问定位
- request对象和response对象
- 20162305 实验二 Java面向对象程序设计 实验报告
- Java类的加载和对象创建流程的详细分析
- Java第三次作业——面向对象基础(封装)
- NullPointerException: android.support.v4.app.FragmentHostCal