gpt4 book ai didi

python - 在 ListView 上测试 get_context_data() 抛出 'AttributeError: object has no attribute ' object_list'

转载 作者:行者123 更新时间:2023-12-04 16:45:17 27 4
gpt4 key购买 nike

我正在尝试在 ListView 上测试我的自定义 get_context_data() 方法,但我一直遇到此错误:AttributeError: 'Home' object has no attribute 'object_list'
该 View 工作正常。测试 View 响应 200 状态代码工作正常。我似乎无法测试我的自定义 get_context_data 方法。

测试

import pytest
from mixer.backend.django import mixer
from django.test import RequestFactory
from django.urls import reverse
from tracker.views import *
from tracker.models import Peak

pytestmark = pytest.mark.django_db


@pytest.fixture
def factory():
return RequestFactory()

def test_number_of_peaks_completed(factory):
mixer.cycle(7).blend(Peak, complete=True)
mixer.cycle(10).blend(Peak)

path = reverse('home')
request = factory.get(path)
view = Home()
view.setup(request)
context = view.get_context_data()

assert context['number_of_peaks_completed'] == 7

看法
from django.views.generic import ListView
from tracker.models import *

class Home(ListView):
model = Peak
template_name = 'tracker/home.html'
context_object_name = 'peaks'

def get_context_data(self, *, object_list=None, **kwargs):
context = super().get_context_data(**kwargs)
context['number_of_peaks_completed'] = Peak.objects.filter(complete=True).count()
return context

错误跟踪
FAILED       [ 16%]
tracker/tests/test_views.py:24 (test_number_of_peaks_completed)
factory = <django.test.client.RequestFactory object at 0x7efbfb8093a0>

def test_number_of_peaks_completed(factory):
mixer.cycle(7).blend(Peak, completed=True)
mixer.cycle(10).blend(Peak, completed=False)

path = reverse('home')
request = factory.get(path)
view = Home()
view.setup(request)
> context = view.get_context_data()

tracker/tests/test_views.py:33:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
tracker/views.py:19: in get_context_data
context = super().get_context_data(**kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <tracker.views.Home object at 0x7efbfb630d00>, object_list = None
kwargs = {}

def get_context_data(self, *, object_list=None, **kwargs):
"""Get the context for this view."""
> queryset = object_list if object_list is not None else self.object_list
E AttributeError: 'Home' object has no attribute 'object_list'

venv/lib/python3.8/site-packages/django/views/generic/list.py:115: AttributeError

最佳答案

问题是您没有使用标准 as_view()功能。

如果您想手动执行此操作,则需要设置 self.object_list你自己(这发生在主 view 函数中,例如 get )。

尝试这个:

def test_number_of_peaks_completed(factory):
mixer.cycle(7).blend(Peak, complete=True)
mixer.cycle(10).blend(Peak)

path = reverse('home')
request = factory.get(path)
view = Home()
view.setup(request)
view.object_list = view.get_queryset()
context = view.get_context_data()

assert context['number_of_peaks_completed'] == 7

您可以将此行移至您自己的 get_context_data以及。

引用: https://github.com/django/django/blob/master/django/views/generic/list.py#L142

关于python - 在 ListView 上测试 get_context_data() 抛出 'AttributeError: object has no attribute ' object_list',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60035194/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com