Django Abstract models set a native name with underscores

I have an abstract base model and 2 inheriting models, and I need to get the associated name to be in a specific format.

class Animal(models.Model):
    legs = models.IntegerField(related_name='%(class)s')
    habitat = models.ForeignKey(Habitats, related_name='%(class)s')

class DogAnimal(BaseModel):
    name = models.CharField(max_length=20, related_name='dog_animal')

class CatAnimal(BaseModel):
    name = models.CharField(max_length=20, related_name='cat_animal')

In the general case, the associated_name = '% (class) s' will lead to a catanilic and pre-gastric one, respectively.

I need to emphasize such values: dog_animal, cat_animal

Here is the β€œWhy” I need to do this - Legacy. These models were not organized with the base class, so the associated name was initially specified: "dog_animal" and "cat_animal". Changing this will be a lot of work.

+4
source share

Source: https://habr.com/ru/post/1599480/


All Articles