Reputation: 423
I have some tables that are presented as inlines of another class. I have altered the default title of these inline representations by adding an inner class to the respective tables.
class Meta:
verbose_name = 'Binnengekomen punten'
I have only the verbose_name defined but it still adds an s to all the names. So 'Binnengekomen punten' is displayed as 'Binnengekomen puntens'
What i could do is define the plural of verbose_name verbose_name_plural
the same as verbose_name. But is there a way to simply turn off the plural notation? I'd love to know thank you.
Upvotes: 1
Views: 749
Reputation: 477513
Adding a suffix is hardcoded, we can see this in the source code:
if self.verbose_name_plural is None: self.verbose_name_plural = format_lazy('{}s', self.verbose_name)
It does not make much sense to write the verbose_name
as 'binnengekomen punten'
(incoming points), since that is plural and a verbose_name
is supposed to be singular. You can however make a decorator that for example automatically defines the plural by adding 'en'
to the verbose_name
, like:
def add_plural(cls, suffix='en'):
if not hasattr(cls, 'verbose_name_plural'):
try:
cls.verbose_name_plural = cls.verbose_name + suffix
except AttributeError:
pass
return cls
We can then use the decorator like:
class IncomingPoint(models.Model):
# ...
@add_plural
class Meta:
verbose_name = 'Binnengekomen punt'
EDIT: You can turn of capitalization, by wrapping the items in a subclass of string, as is shown is in this ticket #18129:
class NoCap(str):
def upper(self):
return self
def __getitem__(self, key):
return self.__class__(super().__getitem__(key))
We can then wrap the verbose_name
and verbose_name_plural
into this:
def case_invariant_meta(cls, suffix='en'):
try:
cls.verbose_name = NoCap(cls.verbose_name)
except AttributeError:
pass
try:
cls.verbose_name_plural = NoCap(cls.verbose_name_plural)
except AttributeError:
pass
return cls
We can then annotate the Meta
class:
class IncomingPoint(models.Model):
# ...
@case_invariant_meta
@add_plural
class Meta:
verbose_name = 'Binnengekomen punt'
Upvotes: 2
Reputation: 168
according to Django docs there isn't method to turn off plural notation.
If verbose_name_plural
isn't given Django uses verbose_name + 's'
Upvotes: 2
Reputation: 8947
I’m sure that it’s possible, but can guarantee you that it would not be worth the effort. Just set the verbose_plural_name
.
Upvotes: 1