如何有效地获得涉及当前类型的多个模型?

时间:2012-02-28 09:19:36

标签: django django-models

我们的一个页面非常缓慢,因为我们正在抓住与一件事有关的类似事情。模型看起来彼此不同,但都与此容器有关:

class MyItem(models.Model):
    deleted = models.BooleanField(default=False)
    whitelabel = models.BooleanField(default=False)
    belongs_in = models.ForeignKey(Container)

class AnotherItem(models.Model):
    deleted = models.BooleanField(default=False)
    belongs_in = models.ForeignKey(Container)
    starts = models.DateTimeField(default=datetime.utcnow())

class ZomgItem(models.Model):
    deleted = models.BooleanField(default=False)
    belongs_in = models.ForeignKey(Container)
    title = models.CharField(max_length=100)

使用这样的容器:

class Container(models.Model):
    title = models.CharField(max_length=100)

    def get_stuff():
        things = list( MyItem.objects.filter(
            belongs_in=self, deleted = False))
        things.extend( AnotherItem.objects.filter(
            belongs_in=self, deleted = False))
        things.extend( ZomgItem.objects.filter(
            belongs_in=self, deleted = False))

        return things

实际使用案例当然更加混乱。我怎么能让这个停止缓慢?

1 个答案:

答案 0 :(得分:0)

如果你说在调用后如何使用“get_stuff”结果会很有帮助。也许只是提出更合适的查询......