Searched the internet a lot for this, tried many solutions but didn't get it working.
I have these models.
models.py
class Cadastro(models.Model):
id_cadastro = models.AutoField(primary_key=True)
categoria_tipo = models.CharField("Categoria", max_length=50, null=True)
chave = models.CharField("Chave", max_length=50, null=False, blank=False)
valor = models.CharField("Valor", max_length=50, null=False, blank=False)
escricao_valor_cadastro = models.TextField("Descrição / Observação", blank=True, null=True)
class Meta:
verbose_name_plural = "Cadastros"
def __str__(self):
return self.valor
class ClasseCNJ(models.Model):
cod_item = models.IntegerField(primary_key=True)
tipo_item = models.CharField(max_length=1)
nome_item = models.CharField(max_length=100)
class Meta:
managed = False
db_table = 'itens'
def __str__(self):
return self.nome_item
class ElementoAutomacao(models.Model):
id_automacao = models.AutoField(primary_key=True)
elemento_automacao = models.ForeignKey('Elemento', on_delete=models.CASCADE, verbose_name="#Elemento", related_name='elemento_automacao', blank=False)
tipo_item = models.ForeignKey('Cadastro', on_delete=models.SET_NULL, related_name='tipo_item', null=True, blank=True,
limit_choices_to={'chave': 'DEST'}, verbose_name="Item Tipo")
item_codigo = models.CharField(max_length=10, blank=True, null=True, verbose_name="#Código")
class Meta:
verbose_name_plural = 'Regras de Automação'
def __str__(self):
return self.elemento_automacao.nome_elemento
def get_item_cnj_desc(self, obj):
if obj.tipo_item == "Movimento":
descr_cnj = ClasseCNJ.objects.get(pk=obj.item_codigo, tipo_item='M')
elif obj.tipo_item == "Classe":
descr_cnj = ClasseCNJ.objects.get(pk=obj.item_codigo, tipo_item='C')
elif obj.tipo_item == "Assunto":
descr_cnj = ClasseCNJ.objects.get(pk=obj.item_codigo, tipo_item='A')
else:
descr_cnj = " - "
return descr_cnj
get_item_cnj_desc.short_description = 'Descrição'
Cadastro model is a generic table that I store key-value data to populate the many listboxes.
ElementoAutomacao has tipo_item field that points to Cadastro, filtering the options, so it can have "Movimento", "Classe" and "Assunto" and item_codigo field that stores a number.
ClasseCNJ is a non-managed model. I use it to get the description associated with the pair tipo_item - item_codigo of ElementoAutomacao model.
admin.py
@admin.register(ElementoAutomacao)
class ElementoAutomacaoRegister(admin.ModelAdmin):
list_display = ('elemento_automacao','tipo_item','operacao','item_codigo','get_item_cnj_desc')
But when I do this I get this error:
get_item_cnj_desc() missing 1 required positional argument: 'obj'
What am I missing?