Django 选择相关的查询
我无法使Django 选择相关的查询,django,django-testing,django-2.1,django-select-related,Django,Django Testing,Django 2.1,Django Select Related,我无法使select\u related使用以下配置: 模型: 作者模型: class Author(models.Model): name = models.CharField(max_length=200) def get_absolute_url(self): kwargs = {'pk': self.pk} return reverse('author-detail', kwargs=kwargs) 看法 在视图中,我使用了select_
select\u related
使用以下配置:
模型:
作者模型:
class Author(models.Model):
name = models.CharField(max_length=200)
def get_absolute_url(self):
kwargs = {'pk': self.pk}
return reverse('author-detail', kwargs=kwargs)
看法
在视图中,我使用了select_related
功能,以避免在查询文本作者时碰到数据库,例如:mytext.author
:
class TextsViewTest(TestCase):
def text_view(request,
pk,
template_name='texts/detail.html'):
source_text = Text.objects.select_related('author').get(pk=pk)
return render(request, template_name,
{
'source': source_text,
})
试验
根据,在访问Text.author
关系时,它不应命中数据库,但在使用以下工具测试时:
def test_layout_content_header__uses_prefetched_relationships(self):
author = Author.objects.create(name="foobar")
source_text = Text.objects.create(author=author)
context = {'source': source_text}
with self.assertNumQueries(0):
from django.template.loader import render_to_string
rendered = render_to_string("text/_content_header.html", context)
模板
text/content\u header.html
:
{% if source.author %} by <em><a href="{{source.author.get_absolute_url}}">{{source.author.name}}</a></em>{% endif %}
有什么想法吗?看起来您没有在测试中使用视图的代码。尝试将同一查询复制到测试中,例如:
context = {'source': Text.objects.select_related('author').get(pk=source_text.pk)}
with self.assertNumQueries(0):
from django.template.loader import render_to_string
rendered = render_to_string("text/_content_header.html", context)
或者重用视图代码(它似乎是在测试用例中声明的,对吗?)
尽管您可能需要指定更高级的请求模拟,例如使用
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_layout_content_header__uses_prefetched_relationships (author.tests.test_views.TextsViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/.../text/tests/test_views.py", line 1035, in test_layout_content_header__uses_prefetched_relationships
source_text.author
File "/.../lib/python3.6/site-packages/django/test/testcases.py", line 80, in __exit__
'%d. %s' % (i, query['sql']) for i, query in enumerate(self.captured_queries, start=1)
AssertionError: 1 != 0 : 1 queries executed, 0 expected
Captured queries were:
1. SELECT "authors_author"."id", "authors_author"."name", FROM "authors_author" WHERE "authors_author"."id" = 1
----------------------------------------------------------------------
Ran 1 test in 0.489s
FAILED (failures=1)
Destroying test database for alias 'default'...
context = {'source': Text.objects.select_related('author').get(pk=source_text.pk)}
with self.assertNumQueries(0):
from django.template.loader import render_to_string
rendered = render_to_string("text/_content_header.html", context)
with self.assertNumQueries(1):
self.text_view(MagicMock(), source_text.pk)