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

Viewed 6462

I'm getting the error CreateView is missing a QuerySet. Define CreateView.model, CreateView.queryset, or override CreateView.get_queryset().

It seems like Django thinks that I'm using CreateView without specifying a model. However, my view does define a model.

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']

The Note model is in 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})

The test I'm using that is raising the error is:

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'
    )

The traceback is:

======================================================================
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().

I've run makemigrations and migrate. I'm using Django 1.9 on Python 3.4. I'm not doing anything unusual, and I've used this exact pattern before without an issue.

EDIT

My 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'),

]
3 Answers

Try changing the URL pattern to:

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

Worked for me.

The problem is in the urls.py and I changed it to the name of my class name in views.py and this worked for me.

I changed the CreateView to my class name in my views.py.

Wrong url:

path('create-task/', views.CreateView.as_view(), name='create-task')

Right url:

path('create-task/', views.TaskCreate.as_view(), name='create-task')

TaskCreate is the name of my class in the views.py and I mistakenly put the CreateView instead of my class name of the views.py ('TaskCreate').

Best of luck!

So I guess you use some kind of auto fill, and you want to pass the viwe class in your url.py but the intellisense put the CreateView in it for you. So in your view module you should import the django.views.generic and import a lot of view class, such as CreateView or ListView, and auto fill just help you fill it in mistankenly.

Related