How to change the `object_name` of a model in Django

I have a LeatherChair object in Django and it pissed me off that Django gives it an object_name of LeatherChair . I want it to be a leather_chair with underline.

It seems that object_name is stored in the Options instance, but how to change this?

+6
source share
1 answer

Trying to change object_name is a really bad idea. It is listed in Model._meta , as you say, but it is not one of the documented metadata available . Trying to change it can cause things to break.

I do not understand why it is so important to change it. It does not appear publicly, so it really doesn't matter if it has an underscore or not. In the comment below, you indicate that object_name used by CBV, but I would use documented ways to change context_object_name in CBV instead of trying to change it on a model

If you need to change it to a model, you probably have to hack django.db.models.options . You can try the DEFAULT_PARAMS monkey patch so that you can set object_name in the Meta model class.

+2
source

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


All Articles