0

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?

  • 1
    The signature `def get_item_cnj_desc(self, obj)` is the required one, if it is a method of the `ModelAdmin`, not the model itself. – user2390182 Apr 28 '20 at 15:50

1 Answers1

0

Based on @schwobaseggl comment, I made some changes and got it working.

In the model.py I now have:

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):

        if self.tipo_item.valor == 'Movimento':
            descr_cnj = ClasseCNJ.objects.get(pk=self.item_codigo, tipo_item='M').nome_item
        elif self.tipo_item.valor == "Classe":
            descr_cnj = ClasseCNJ.objects.get(pk=self.item_codigo, tipo_item='C').nome_item
        elif self.tipo_item.valor == "Assunto":
            descr_cnj = ClasseCNJ.objects.get(pk=self.item_codigo, tipo_item='A').nome_item
        else:
            descr_cnj = " -sa "

        return descr_cnj

and in the admin.py

@admin.register(ElementoAutomacao)
class ElementoAutomacaoRegister(admin.ModelAdmin):
    list_display = ('elemento_automacao','tipo_item','operacao','item_codigo','get_item_cnj_desc')

    def get_item_cnj_desc(self,obj):
       return obj.get_item_cnj_desc()

    get_item_cnj_desc.short_description = 'Descrição Item'  # Renames column head

Thanks!