How to change inlines according to an option in Django admin

Asked

Viewed 18 times

0

Currently I have the following code:

@admin.register(Experiencia)
class ExperienciaAdmin(admin.ModelAdmin):
    list_display = ('tipo', 'data_inicio', 'data_fim')

    inlines = [AcademicoInline, ProfissionalInline]

    def get_inline_instances(self, request, obj=None):
        pass

I’d like to register a Experiencia for a user, but the user can choose only one, Academica or Profissional. I’d like to use the field tipo of Experiencia to do this, when the user chooses the type academica, show AcademicaInline, when the user selects profissional, show ProfissionalInline. this record needs to be done using the Django administration module.

I tried to use get_inline_instances, but all my attempts failed.

but models are like this:

class Experiencia(models.Model):
    ACADEMICA = 'AC'
    PROFISSIONAL = 'PR'
    TIPO_CHOICES = [
        (ACADEMICA, 'Acadêmica'),
        (PROFISSIONAL, 'Profissional'),
    ]
    tipo = models.CharField('Tipo', choices=TIPO_CHOICES, max_length=2)
    data_inicio = models.DateField('Data de início')
    data_fim = models.DateField('Data fim')


class Profissional(models.Model):
    empresa = models.CharField('Empresa', max_length=200)
    cargo = models.CharField('Cargo', max_length=200)
    remuneracao = models.FloatField('Remuneração')
    atividade = models.TextField('Atividade')
    experiencia = models.OneToOneField(Experiencia, on_delete=models.CASCADE)


class Academica(models.Model):
    instituicao = models.CharField('Instituição', max_length=200)
    curso = models.CharField('Curso', max_length=200)
    experiencia = models.OneToOneField(Experiencia, on_delete=models.CASCADE)
  • I’ve seen your new post now. I think it’s that that you want.

  • That would solve by the front, I wanted in the admin panel of Django, I could not solve, the only way I found is to save the object and then edit again, then I can see the academic or professional field according to the type informed.

  • Who? Wouldn’t point out the form in Experienceadmin? Maybe that help

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.