如果我在没有自定义管理器的情况下执行以下所有操作,则一切都按预期工作:
class Content(models.Model):
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Meta:
app_label = 'game'
class Requirement(models.Model):
content = models.ForeignKey(Content, on_delete=models.CASCADE, related_name = 'requirements')
value = models.IntegerField(default=0)
def __str__(self):
return "{} requires value {}".format(self.content,self.value)
class Meta:
app_label = 'game'
def testPrefetchOrig():
contents = Content.objects.filter(name__startswith = 'a').prefetch_related('requirements')
for c in contents:
for r in c.requirements.all():
print r
logging.warning(r)
logging.warning("Done with query")
这一次预取数据,再也不会:
DEBUG:django.db.backends:(0.001) SELECT "game_content"."id", "game_content"."name", "game_content"."deleted" FROM "game_content" WHERE "game_content"."name"::text LIKE 'a%'; args=(u'a%',)
DEBUG:django.db.backends:(0.001) SELECT "game_requirement"."id", "game_requirement"."content_id", "game_requirement"."value", "game_requirement"."deleted" FROM "game_requirement" WHERE "game_requirement"."content_id" IN (5, 6); args=(5, 6)
alphabet requires value 5
WARNING:root:alphabet requires value 5
alphabet requires value 3
WARNING:root:alphabet requires value 3
albatross requires value 1
WARNING:root:albatross requires value 1
albatross requires value 0
WARNING:root:albatross requires value 0
WARNING:root:Done with query
但是,我想使用自定义管理器来处理已删除的条目的过滤'通过设置已删除的标志。
class DeletedItemsQuerySet(models.query.QuerySet):
def get(self, *args, **kwargs):
kwargs['deleted']=False
return models.query.QuerySet.get(self, *args, **kwargs)
def all(self):
return self.filterNoDeleted()
def filterNoDeleted(self, *args, **kwargs):
kwargs['deleted']=False
return models.query.QuerySet.filter(self, *args, **kwargs)
def getDeleted(self, *args, **kwargs):
return models.query.QuerySet.get(self, *args, **kwargs)
def filterDeleted(self, *args, **kwargs):
return models.query.QuerySet.filter(self, *args, **kwargs)
class DeletedItemsManager(models.Manager.from_queryset(DeletedItemsQuerySet)):
def all(self):
return super(models.Manager,self).all().filterNoDeleted()
然后我们修改我们的模型来使用它:
class Content(models.Model):
name = models.CharField(max_length=200)
objects = DeletedItemsManager()
deleted = models.BooleanField(default=False)
def __str__(self):
return self.name
class Meta:
app_label = 'game'
class Requirement(models.Model):
content = models.ForeignKey(Content, on_delete=models.CASCADE, related_name = 'requirements')
value = models.IntegerField(default=0)
deleted = models.BooleanField(default=False)
objects = DeletedItemsManager()
def __str__(self):
return "{} requires value {}".format(self.content,self.value)
class Meta:
app_label = 'game'
def testPrefetchOrig():
contents = Content.objects.filter(name__startswith = 'a').prefetch_related('requirements')
for c in contents:
for r in c.requirements.all():
print r
logging.warning(r)
logging.warning("Done with query")
这会预取数据,但仍会查询数据:
DEBUG:django.db.backends:(0.001) SELECT "game_content"."id", "game_content"."name", "game_content"."deleted" FROM "game_content" WHERE "game_content"."name"::text LIKE 'a%'; args=(u'a%',)
DEBUG:django.db.backends:(0.001) SELECT "game_requirement"."id", "game_requirement"."content_id", "game_requirement"."value", "game_requirement"."deleted" FROM "game_requirement" WHERE "game_requirement"."content_id" IN (5, 6); args=(5, 6)
DEBUG:django.db.backends:(0.000) SELECT "game_requirement"."id", "game_requirement"."content_id", "game_requirement"."value", "game_requirement"."deleted" FROM "game_requirement" WHERE ("game_requirement"."content_id" = 5 AND "game_requirement"."deleted" = false); args=(5, False)
alphabet requires value 5
WARNING:root:alphabet requires value 5
alphabet requires value 3
WARNING:root:alphabet requires value 3
DEBUG:django.db.backends:(0.001) SELECT "game_requirement"."id", "game_requirement"."content_id", "game_requirement"."value", "game_requirement"."deleted" FROM "game_requirement" WHERE ("game_requirement"."content_id" = 6 AND "game_requirement"."deleted" = false); args=(6, False)
albatross requires value 1
WARNING:root:albatross requires value 1
albatross requires value 0
WARNING:root:albatross requires value 0
WARNING:root:Done with query
如何使用自定义管理器并仍然具有prefetch_related工作?
答案 0 :(得分:0)
好。 DeletedItemsManager在每个all()方法调用上添加一个额外的filterNoDeleted()方法调用。根据{{3}}
中的注释,该过滤器调用会破坏缓存但是,我确实需要过滤掉已删除的相关对象。
我应该能够在Requirement表中选择所有必需的行,但我不确定如何在Queryset中设置正确的字段以反映这些结果。但是,如果我只想阅读结果,我可以把它变成字典。
有没有人有更优雅的解决方案?
注意:有一个对Django的filtered_relation添加的pull请求,但是这对prefetch_related还没有用。也许将来Django会支持这个用例。
答案 1 :(得分:0)
def testPrefetchOrig():
contents = Content.objects.filter(name__startswith = 'a').prefetch_related(Prefetch('requirements', queryset=Requirement.objects.filterNoDeleted(),to_attr='undeletedRequirements'))
for c in contents:
for r in c.undeletedRequirements:
print r
print "Done with query"