[Answered ]-How to Change OneToOne model Field Default Message in Django Admin?

1👍

You can override the error_messages=… parameter [Django-doc] for the unique error message:

class Replied(BaseModel):
    reply = models.OneToOneField(
        Review,
        on_delete=models.CASCADE,
        error_messages={'unique': 'Reply to this review already exists.'}
    )
    show_reply = models.BooleanField(default=False)
    replied_by = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Reply'
        verbose_name_plural = 'Replies'

    def __str__(self):
        return self.content

That being said, the default message is:

,code>'unique': '%(model_name)s with this %(field_label)s already exists.'

so by setting the verbose_name=… [Django-doc], this issue is also resolved, and all other error messages will be adapted:

class Replied(BaseModel):
    reply = models.OneToOneField(
        Review,
        on_delete=models.CASCADE,
        verbose_name='review'
    )
    show_reply = models.BooleanField(default=False)
    replied_by = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        verbose_name = 'Reply'
        verbose_name_plural = 'Replies'

    def __str__(self):
        return self.content

Leave a comment