我收到错误CreateView is missing a QuerySet. Define CreateView.model, CreateView.queryset, or override CreateView.get_queryset().
似乎Django认为我在没有指定模型的情况下使用CreateView。但是,我的观点确实定义了一个模型。
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并进行迁移。我在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'),
]
答案 0 :(得分:2)
尝试将网址格式更改为:
url(r'^note/create', views.CreateNoteView.as_view(), name='note_create')
为我工作。