class Banco(models.Model):
numero = PositiveSmallIntegerField(db_column='Numero', verbose_name='Número', primary_key=True)
nome_razao_social = CharField(db_column='NomeRazaoSocial', verbose_name='Razão Social', max_length=100)
url = models.CharField(db_column='URL', max_length=100, verbose_name='URL', blank=True, null=True)
# Metadata
class Meta:
# managed = False
db_table: str = 'tb_bancos'
verbose_name: str = 'Banco'
verbose_name_plural: str = 'Bancos'
db_table_comment: str = 'Cadastro de bancos'
ordering: list[str] = ['numero']
def __str__(self) -> CharField:
""" String para representar o objeto MyModelName (no site Admin)."""
return self.nome_razao_social
def __list_display__(self) -> tuple:
return (self.numero,
self.nome_razao_social,
self.url)
def __search_fields__(self) -> tuple:
return (self.nome_razao_social,)
def __list_filter__(self) -> tuple:
return (self.nome_razao_social,)
How do I use the list_display, search_fields and list_filter functions in the code below?
from django.contrib import admin
from .models import (Banco)
tabelas_dominios_models: list = ['Banco']
for _ in tabelas_dominios_models:
eval(_).__str__(eval(_))
@admin.register(eval(_))
class TabelaDominioAdmin(admin.ModelAdmin):
list_display: tuple = eval(_).__list_display__(eval(_))
search_fields: tuple = eval(_).__search_fields__(eval(_))
list_filter: tuple = eval(_).__list_filter__(eval(_))
When I run the code I get the error below:
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\PRSantos\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1075, in _bootstrap_inner
self.run()
File "C:\Users\PRSantos\AppData\Local\Programs\Python\Python312\Lib\threading.py", line 1012, in run
self._target(*self._args, **self._kwargs)
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\core\management\commands\runserver.py", line 134, in inner_run
self.check(**check_kwargs)
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\core\management\base.py", line 492, in check
all_issues = checks.run_checks(
^^^^^^^^^^^^^^^^^^
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\core\checks\registry.py", line 89, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\contrib\admin\checks.py", line 52, in check_admin_app
errors.extend(site.check(app_configs))
^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\contrib\admin\sites.py", line 90, in check
errors.extend(modeladmin.check())
^^^^^^^^^^^^^^^^^^
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\contrib\admin\options.py", line 151, in check
return self.checks_class().check(self, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\contrib\admin\checks.py", line 810, in check
*self._check_list_display(admin_obj),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\contrib\admin\checks.py", line 899, in _check_list_display
return list(
^^^^^
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\contrib\admin\checks.py", line 901, in <genexpr>
self._check_list_display_item(obj, item, "list_display[%d]" % index)
File "D:\Users\PRSantos\Desenvolvimentos\ProjetosPython\ProjetosPRSantos\pjtSisAdminWeb2025\.venvPjtSisAdminWeb2025\Lib\site-packages\django\contrib\admin\checks.py", line 909, in _check_list_display_item
elif hasattr(obj, item):
^^^^^^^^^^^^^^^^^^
TypeError: attribute name must be string, not 'DeferredAttribute'