TypeError: 'NoneType' object is not callable (DjangoAdmin)

I am trying to register one of my Models in the Django admin page (code below):

class Collection(models.Model):
    title = models.CharField(max_length=255)
    featured_product = models.ForeignKey(
        'Product', on_delete=models.SET_NULL, null=True, related_name='+')

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['title']

code from admin.py:

class CollectionAdmin(admin.ModelAdmin):
    list_display = ['title', 'products_count']

    def products_count(self, collection):
        return collection.products_count

    def get_queryset(self, request):
        return super().get_queryset(request).annotate(products_count=Count('product'))

But I get this error for the collection Model for some reason and for no other model that i registered.


Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 980, in _bootstrap_inner
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\threading.py", line 917, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\core\management\commands\runserver.py", line 125, in inner_run
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\utils\autoreload.py", line 87, in raise_last_exception
    raise _exception[1]
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\core\management\__init__.py", line 394, in execute
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\__init__.py", line 24, in setup
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\apps\registry.py", line 124, in populate
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\contrib\admin\apps.py", line 27, in ready
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\contrib\admin\__init__.py", line 50, in autodiscover
    autodiscover_modules("admin", register_to=site)
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\djangoproject\venv\lib\site-packages\django\utils\module_loading.py", line 58, in autodiscover_modules
    import_module("%s.%s" % (app_config.name, module_to_search))
  File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\jonas_6xnnk8f\PycharmProjects\storefront\store\admin.py", line 7, in <module>
    class CollectionAdmin(admin.ModelAdmin):
TypeError: 'NoneType' object is not callable

Does anyone know what I am doing wrong?

In admin.py you are using count on foreign key i.e featured_product and you have given related_name as + in your model. You might be getting error cause of this, try related_name as product in your model or instead of this products_count=Count('product')) use products_count=Count('featured_product')) in admin.py.

Unfortunately didn’t work, but thanks anyway.

There are a couple possibilities that I see here-

  • You’re using the name products_count both as a method name in the admin class and as the annotation name of a data field in the queryset. As documented at the end of the ModelAdmin.list_display section of the docs, it’s going to look for the model field first, but I’m not sure if that applies to an annotation field.

  • Same thought regarding the use of the name products_count in the decorator.

My first suggestion then would be to “deconfuse” the situation by selecting unique names for these references to make it unambiguous as to which entities are being referenced.

1 Like

Fixed the error. The Problem was that I typed @admin.site.register(models.Collection) instead of @admin.register(models.Collection). Can’t believe that I didn’t notice this earlier.
But thanks for your help anyway.

1 Like