Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited

Tags:

python

django

(ll_env) brads-MacBook-Pro:learning_log $ python manage.py runserver
    Performing system checks...
    
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d417d08>
    Traceback (most recent call last):
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
        fn(*args, **kwargs)
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
        self.check(display_num_errors=True)
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/management/base.py", line 379, in check
        include_deployment_checks=include_deployment_checks,
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/management/base.py", line 366, in _run_checks
        return checks.run_checks(**kwargs)
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/checks/registry.py", line 71, in run_checks
        new_errors = check(app_configs=app_configs)
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/checks/urls.py", line 13, in check_url_config
        return check_resolver(resolver)
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/core/checks/urls.py", line 23, in check_resolver
        return check_method()
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 396, in check
        for pattern in self.url_patterns:
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 533, in url_patterns
        patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/utils/functional.py", line 37, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/urls/resolvers.py", line 526, in urlconf_module
        return import_module(self.urlconf_name)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 978, in _gcd_import
      File "<frozen importlib._bootstrap>", line 961, in _find_and_load
      File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 678, in exec_module
      File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
      File "/learning_log/learning_log/urls.py", line 23, in <module>
        url(r'',include('learning_logs.urls',namespace='learning_logs')),
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/urls/conf.py", line 34, in include
        urlconf_module = import_module(urlconf_module)
      File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 978, in _gcd_import
      File "<frozen importlib._bootstrap>", line 961, in _find_and_load
      File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 678, in exec_module
      File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
      File "/learning_log/learning_logs/urls.py", line 2, in <module>
        from . import views
      File "/learning_log/learning_logs/views.py", line 6, in <module>
        from .forms import TopicForm
      File "/learning_log/learning_logs/forms.py", line 4, in <module>
        class TopicForm(forms.ModelForm):
      File "/learning_log/ll_env/lib/python3.6/site-packages/django/forms/models.py", line 243, in __new__
        "needs updating." % name
    django.core.exceptions.ImproperlyConfigured: Creating a ModelForm without either the 'fields' attribute or the 'exclude' attribute is prohibited; form TopicForm needs updating.




 

some part of the code:

manage.py

#!/usr/bin/env python
import os
import sys

if __name__ == '__main__':
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'learning_log.settings')
    try:
        from django.core.management import execute_from_command_line
    except ImportError as exc:
        raise ImportError(
            "Couldn't import Django. Are you sure it's installed and "
            "available on your PYTHONPATH environment variable? Did you "
            "forget to activate a virtual environment?"
        ) from exc
    execute_from_command_line(sys.argv)

views.py

from django.shortcuts import render
from .models import Topic
from django.http import HttpResponseRedirect
#from django.core.urlresolvers import reverse
from django.urls import reverse
from .forms import TopicForm
    
# Create your views here.
def index(request):
    return render(request,'learning_logs/index.html')

def topics(request):
    topics=Topic.objects.order_by('date_added')
    context={'topics':topics}
    return render(request,'learning_logs/topics.html',context)
    
def topic(request,topic_id):
    topic=Topic.objects.get(id=topic_id)
    entries=topic.entry_set.order_by('-date_added')
    context={'topic':topic,'entries':entries}
    return render(request,'learning_logs/topic.html',context)
    
def new_topic(request): 
    if request.method != 'POST': 
        form = TopicForm()
    else: 
        form = TopicForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('learning_logs:topics'))
    
    context = {'form',form}
    return render(request,'learning_logs/new_topic.html',context)

So, I'ver been learning Python using the book Python Crash Course: A hands-on, Project-Based Introduction to Programming. And when I 'runserver', it just showed the above error. I don't know what was wrong, because I did exactly what the book says. Can anybody help me out?

what happened when I try the above steps.

Any ideas?

like image 507
Existence Avatar asked Aug 07 '18 19:08

Existence


3 Answers

When you are creating a form using model, you need to specify which fields you want to be included in the form.

For example you have a model with the named Article and you want to create a form for the model article.

pub_date, headline, content, reporter these are the fields in the model. If you choose to include all the fields you do like this.

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = '__all__'

If you want to specify which fields you want to include

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        fields = ['pub_date', 'headline', 'content']

If you want to exclude a certain fields and use the remaining then use as follows

class ArticleForm(ModelForm):
    class Meta:
        model = Article
        exclude = ['headline']

Coming to your error, it says you can use ModelForm with out using one of the two options that is using fields or exclude.

like image 178
kartheek Avatar answered Nov 18 '22 11:11

kartheek


class TopicForm(forms.ModelForm):
    class Meta:
        model = Topic
        fields = '__all__' # or whatever fields you want ('field_a', )
like image 6
Kapil Lamichhane Avatar answered Nov 18 '22 11:11

Kapil Lamichhane


I had the exact same error and turns out I had written 'field' instead of 'fields '

like image 3
Michelle Claire Avatar answered Nov 18 '22 10:11

Michelle Claire