Edit behavior of a table in Django 2.2 admin

I am currently implementing an application in which the model has the following tables defined:

# Modelo cactaceas
class Taxonomia(models.Model): 
    id_cactacea = models.AutoField(primary_key=True)
    subfamilia = models.CharField(max_length=200)
    genero = models.CharField(max_length=200)
    especie = models.CharField(max_length=200)
    subespecie = models.CharField(max_length=200, null=True, blank=True)
    autor = models.CharField(max_length=200)
    descripcion = models.TextField(null=True, blank=True)
        ('NE', 'no evaluada'),
        ('DD', 'informacion insuficiente'),
        ('LC', 'menor preocupacion'),
        ('NT', 'casi amenazado'),
        ('VU', 'vulnerable'),
        ('EN', 'en peligro de extension'),
        ('CR', 'en peligro critico'),
        ('EW', 'extinto en la naturaleza'),
        ('EX', 'extinto'),
    estado_conservacion = models.CharField(max_length=2, choices=ESTADO_CONSERVACION, default='NE')

    distribuciones = models.ManyToManyField(Distribucion)
    fenologias = models.ManyToManyField(Fenologia)

    imagenes = models.ForeignKey(Imagen, on_delete=models.CASCADE, default=1)

    class Meta:
        ordering = ["-subfamilia"]

    def get_absolute_url(self):
        from django.urls import reverse
        return reverse("detail-cactacea", args=[int(self.id_cactacea)])

    def __str__(self):

        subespecie = ""
        if self.subespecie != None:
            subespecie = self.subespecie

        cactacea = self.subfamilia + " " + self.genero + " " + self.especie + " " + subespecie + " " + self.autor
        return cactacea

# Modelo distribucion
class Distribucion(models.Model):
    id_distribucion = models.AutoField(primary_key=True)
    localidad = models.TextField(null=True, blank=True)

    def __str__(self):

        return self.localidad

I am using the “admin.ModelAdmin” subclass for rendering the models in the django admin area getting the following rendering for each record in the “taxonomia” table

As you can see in the image above this shows a box with a list of all the records defined in the “distribucion” table and at the end “+” button to add new records to the “distribucion” table.

What I need is that by default for this section only the list of items in the “distribucion” table that are already associated with the record of the “taxonomia” table that I am editing is displayed, and in case it does not already have any records associated nothing appears. Example:

Also that the “+” button is still maintained to be able to add records to the “distribucion” table directly and that these when recording are directly associated with the table item “taxonomy” that I am editing.

I would be grateful if you could indicate that part of the django 2.2 documentation I should review, since up to now I have not been able to locate a part in the edition of the class “admin.ModelAdmin” or in the section on the model, which allows me to generate this behavior.

You should take a look to TabularInline.

1 Like

Thanks @marcorichetta, I managed to get the desired result.