I have the following models:

from django.db import models
from foo.bar.models import Location


class AbstractShop(models.Model):
    location = models.ForeignKey(Location, on_delete=models.CASCADE, related_name="%(class)s")

    class Meta(self):
        abstract = True


class Bakery(AbstractShop):
    some_field = models.BooleanField("Some field", default=True)

    class Meta:
        verbose_name = "Bakery"
        verbose_name_plural = "Bakeries"


class Supermarket(AbstractShop):
    some_other_field = models.CharField("Some other field", max_length=20)

    class Meta:
        verbose_name = "Supermarket"
        verbose_name_plural = "Supermarkets"

Now, Supermarket as well as Bakery inherit the location-ForeignKey from AbstractShop.
If I want to query the reverse relation to Bakery on the Location model, I would have to use bakerys (instead of the correct bakeries) as related_name - which I don't want as it's grammatically wrong and unintuitive.

So my questions are:

  1. Is there any way to use the verbose_name_plural as related_name?

  2. Is there any way at all to use any other related_name than "%(class)s" or "%(app_label)s or do I just have to implement the ForeignKey on the child classes?

If so, that would be somewhat annoying. Imagine you have a lot of shared ForeignKeys: You can move the ones with regular plurals in English to the abstract base class (ABC) (because for regular nouns the added "s" in "%(class)s" results in the correct plural form) while those with irregular plurals have to be implemented on the child class (as only there the related_name can be set to the plural of the actual name of the child class, which you don't know in the ABC).
This is a totally arbitrary condition which might not be obvious to non-native English speakers, also it transfers linguistic logic to code, which IMHO shouldn't happen.

0

There are 0 answers