LeoTunero
LeoTunero

Reputation: 1

Django3.2.6 : TypeError: 'NoneType' object is not callable

I am learning this Django Framework from this summer. I have get the error <TypeError: 'NoneType' object is not callable>. I am trying to narrow down the problem. However, after I have delete a lot of code, I still cannot figure this out from the most simple code. My code is showed as below.

admin.py

from django.contrib import admin
from rest_framework.renderers import AdminRenderer

from .models import Car


@admin.site.register(Car)
class CarAdmin(admin.ModelAdmin):
    list_display = ('id', 'brand', 'model')

models.py

from django.db import models
from django.contrib import admin


class Car(models.Model):
    brand = models.TextField(default='Honda')
    model = models.TextField(default='EK9')

    class Meta:
        db_table = 'car'

    def __str__(self):
        return self.model

the error

LeodeMacBook-Pro:sharky leo$ python ./manage.py runserver
/Users/leo/opt/anaconda3/lib/python3.8/site-packages/environ/environ.py:628: UserWarning: /Users/leo/Documents/python_test/leo_first_python_side_project/sharky/sharky/mysite/.env doesn't exist - if you're not configuring your environment separately, create one.
  warnings.warn(
/Users/leo/opt/anaconda3/lib/python3.8/site-packages/environ/environ.py:628: UserWarning: /Users/leo/Documents/python_test/leo_first_python_side_project/sharky/sharky/mysite/.env doesn't exist - if you're not configuring your environment separately, create one.
  warnings.warn(
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/Users/leo/opt/anaconda3/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/Users/leo/opt/anaconda3/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/leo/django/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/leo/django/django/core/management/commands/runserver.py", line 114, in inner_run
    autoreload.raise_last_exception()
  File "/Users/leo/django/django/utils/autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "/Users/leo/django/django/core/management/__init__.py", line 375, in execute
    autoreload.check_errors(django.setup)()
  File "/Users/leo/django/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "/Users/leo/django/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/leo/django/django/apps/registry.py", line 122, in populate
    app_config.ready()
  File "/Users/leo/django/django/contrib/admin/apps.py", line 27, in ready
    self.module.autodiscover()
  File "/Users/leo/django/django/contrib/admin/__init__.py", line 24, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/Users/leo/django/django/utils/module_loading.py", line 47, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/Users/leo/opt/anaconda3/lib/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/leo/Documents/python_test/leo_first_python_side_project/sharky/sharky/car/admin.py", line 8, in <module>
    class CarAdmin(admin.ModelAdmin):
TypeError: 'NoneType' object is not callable

environment

asgiref==3.4.1; python_version >= '3.6'
django-environ==0.4.5
django==3.2.6
pytz==2021.1
sqlparse==0.4.1; python_version >= '3.5'

what I have down

I have search the web of Django & Python and dozens of Questions on stack overflow. I have realize that the most common solution is to remove the (). However, I do not call the func. anywhere. Therefore,I have raised this question to search for is there some mistake I do not understand about. Thanks for any advice/suggestion that help me to improve.

Upvotes: 0

Views: 794

Answers (1)

Dave Mcsavvy
Dave Mcsavvy

Reputation: 520

The error is from the @admin.site.register(Car) this is because it returns None and wrapping CarAdmin with None is equivalent to doing CarAdmin = None(CarAdmin).

The solution is to use @admin.register(Car) instead

Upvotes: 3

Related Questions