我们的一个页面非常缓慢,因为我们正在抓住与一件事有关的类似事情。模型看起来彼此不同,但都与此容器有关:
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
实际使用案例当然更加混乱。我怎么能让这个停止缓慢?
答案 0 :(得分:0)
如果你说在调用后如何使用“get_stuff”结果会很有帮助。也许只是提出更合适的查询......