2

I know that a model object string representation can be achieved by adding

class Company(models.Model):
    name = models.CharField()
    email = models.EmailField(unique=True)

    def __str__(self):
        return self.name

But this is the representation of an object of Company but not the model class itself. Meaning if I create an object

obj = Company(name='string_repr', email='example@example.com')

print(obj) would result in 'string_repr' which is as expected since Im getting the string representation of the object, not the class/ model. However I noticed that during a CreateView form validation in case if there already is an object of the model with the same email Django returns an error message that there already is a Company with such an email (picture is not in English but the underlined section is the only relevant part).

enter image description here

I suppose the error message is taking the class name and inputting it in the message, but is there a way how to alter the string representation of the model (class) name? Its needed for translation purposes. I guess I could just rename the class names to local language but that doesn't seem right.

d1spstack
  • 930
  • 1
  • 5
  • 16
  • 2
    Does this answer your question ? https://stackoverflow.com/a/1488660/152016 – Niloct Nov 01 '21 at 14:21
  • 1
    @Niloct I suppose it does regarding the error message content, although I'm still curious about the string representation of the model itself – d1spstack Nov 02 '21 at 09:17

0 Answers0