Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 选择相关的查询_Django_Django Testing_Django 2.1_Django Select Related - Fatal编程技术网

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)