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.
  • 对象A与其自身具有非对称的多对多关系。

  • Object B has a many-to-one relationship with Object A.
  • 对象B与对象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.
  • 给定一个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

另请参阅跨越关系的查找

更多相关文章

  1. PostgreSQL操纵大对象(图片等)
  2. Recordset记录集对象的属性
  3. GenericObjectPool对象池异常排查
  4. Java对象创建的过程及对象的内存布局与访问定位
  5. request对象和response对象
  6. 20162305 实验二 Java面向对象程序设计 实验报告
  7. Java类的加载和对象创建流程的详细分析
  8. Java第三次作业——面向对象基础(封装)
  9. NullPointerException: android.support.v4.app.FragmentHostCal

随机推荐

  1. android关于获取手机唯一码的组合文章
  2. 【android】get PatchStore::createDisab
  3. Android软键盘手动显示、隐藏、布局上移
  4. Android(安卓)TableLayout表格布局
  5. Android aapt 工具介绍(转载)
  6. Android 下载文件 显示进度条
  7. 时钟控件布局
  8. Android(安卓)通过字符串来获取R下面资源
  9. Android studio报错:找不到匹配的任何版本
  10. Android 利用Java实现压缩与解压缩(zip、g