How to reuse properties from table with django for admin?

oke. I have one table. like:

class AnimalGroup(models.Model):
    name = models.CharField(max_length=50, unique=True)  
    description = models.TextField(max_length=1000, blank=True)
    images = models.ImageField(upload_to="photos/groups")
    

    class Meta:
        verbose_name = "animalgroup"
        verbose_name_plural = "animalgroups"

    def __str__(self):
        return self.group_name

And this are animal groups. Like:

  • mammals
  • fish
  • reptils

But then I hava a subgroup where a user can enter the same properties as by AnimalGroup. And the AnimalGroup can have multiple subgroups. Like:

mammals: cats, dogs, cattles.

And then you have the animal it self:

  • cats: siamese cat, ragdoll, etc.

My question is: how can I resuse this properties for the three objects? So

  • AnimalGroup
  • subgroup
  • animal

So that a user can enter in the admin panel of django:

  • mammals
  • fish
  • reptils

and then can enter cats and choose from dropdown mammals.

Because all the objects:

  • AnimalGroup
  • subgroup
  • animal

have in common the fields:

  • name
  • description
  • images

If I do it like.This is of course not very efficient:

class AnimalGroup(models.Model):
    name = models.CharField(max_length=50, unique=True)  
    description = models.TextField(max_length=1000, blank=True)
    images = models.ImageField(upload_to="photos/groups")



class Subgroup(models.model)
    name = models.CharField(max_length=50, unique=True)  
    description = models.TextField(max_length=1000, blank=True)
    images = models.ImageField(upload_to="photos/groups")
    animalGroup= models.ForeignKey(AnimalGroup, on_delete=models.CASCADE) 

class Animal(models.model)
    name = models.CharField(max_length=50, unique=True)  
    description = models.TextField(max_length=1000, blank=True)
    images = models.ImageField(upload_to="photos/groups")
    subgroup= models.ForeignKey(Subgroup, on_delete=models.CASCADE) 

Back to Top