gpt4 book ai didi

django.core.exceptions.ImproperlyConfigured : CreateView is missing a QuerySet

转载 作者:行者123 更新时间:2023-12-05 01:46:22 25 4
gpt4 key购买 nike

我收到错误 CreateView 缺少 QuerySet。定义 CreateView.model、CreateView.queryset 或覆盖 CreateView.get_queryset()。

Django 似乎认为我在使用 CreateView 时没有指定模型。但是,我的 View 确实定义了一个模型。

views.py:

from django.views.generic.edit import CreateView
from django.contrib.auth.mixins import PermissionRequiredMixin
from .models import Note

class CreateNoteView(PermissionRequiredMixin, CreateView):
model = Note
permission_required = 'file_manager.can_add_note'
template_name = 'file_manager/note_create.html'
fields = ['title', 'note', 'tags', 'cases', 'people']

Note 模型在 models.py 中:

class Note(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created = models.DateTimeField(auto_now_add=True, null=True)
updated = models.DateTimeField(auto_now=True, null=True)
title = models.CharField(max_length=60)
note = models.TextField(null=True, blank=True)
tags = models.ManyToManyField(FileManagerTags, related_name='tagged_note_set')
cases = models.ManyToManyField(Case, related_name='related_note_set')
people = models.ManyToManyField(Person, related_name='notes_rel_to_person')

def __str__(self):
return str(self.title)

def get_absolute_url(self):
return reverse('file_manager:note_create', kwargs={'pk': self.pk})

我正在使用的引发错误的测试是:

def test_whether_note_create_view_uses_correct_template(self):
client = Client()
test_superuser = User.objects.get(username=test_superuser_username)
client.force_login(test_superuser)
response = client.get(reverse('file_manager:note_create'), follow=True)
self.assertTemplateUsed(
response=response,
template_name='file_manager/note_index.html'
)

回溯是:

======================================================================
ERROR: test_whether_note_create_view_uses_correct_template (file_manager.tests.test_views.NoteCreateView)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/mint/Python_Projects/[project_name]/file_manager/tests/test_views.py", line 72, in test_whether_note_create_view_uses_correct_template
response = client.get(reverse('file_manager:note_create'))
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/test/client.py", line 503, in get
**extra)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/test/client.py", line 304, in get
return self.generic('GET', path, secure=secure, **r)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/test/client.py", line 380, in generic
return self.request(**r)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/test/client.py", line 467, in request
six.reraise(*exc_info)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/utils/six.py", line 686, in reraise
raise value
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/core/handlers/base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/core/handlers/base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/views/generic/edit.py", line 251, in get
return super(BaseCreateView, self).get(request, *args, **kwargs)
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/views/generic/edit.py", line 212, in get
return self.render_to_response(self.get_context_data())
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/views/generic/edit.py", line 121, in get_context_data
kwargs.setdefault('form', self.get_form())
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/views/generic/edit.py", line 73, in get_form
form_class = self.get_form_class()
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/views/generic/edit.py", line 152, in get_form_class
model = self.get_queryset().model
File "/home/mint/Python_Projects/virtualenvs/[project_name]/lib/python3.4/site-packages/django/views/generic/detail.py", line 74, in get_queryset
'cls': self.__class__.__name__
django.core.exceptions.ImproperlyConfigured: CreateView is missing a QuerySet. Define CreateView.model, CreateView.queryset, or override CreateView.get_queryset().

我已经运行了 makemigrations 和 migrate。我在 Python 3.4 上使用 Django 1.9。我没有做任何不寻常的事情,而且我以前使用过这个确切的模式没有问题。

编辑

我的 urls.py:

from django.conf.urls import url, include

from . import views

urlpatterns = [

# Note urls
url(r'^note/create', views.CreateView.as_view(), name='note_create'),
url(r'^notes/$', views.NoteIndexView.as_view(), name='note_index'),

]

最佳答案

尝试将 URL 模式更改为:

url(r'^note/create', views.CreateNoteView.as_view(), name='note_create')

为我工作。

关于django.core.exceptions.ImproperlyConfigured : CreateView is missing a QuerySet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35960152/

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