开发者

How to change `object_name` of a model in Django

开发者 https://www.devze.com 2023-04-11 08:17 出处:网络
I have an object LeatherChair in Django, and it pisses me off that Django gives it an object_name of leatherchair. I want it to be leather_chair, with an underscore.

I have an object LeatherChair in Django, and it pisses me off that Django gives it an object_name of leatherchair. I want it to be leather_chair, with an underscore.

The ob开发者_开发百科ject_name seems to be stored in the Options instance, but how do I change that?


Trying to change object_name is a really bad idea. It is set in the Model._meta as you say, but it is not one of the documented available Meta options. Trying to change it risks breaking stuff.

I don't understand why it is so important to change it. It's not displayed publicly, so it doesn't really matter whether it has an underscore or not. In the comment below you point out that object_name is used by the CBV, but I would use the documented ways to change context_object_name in the CBV instead of trying to change it on the model

If you must change it on the model, you probably need to hack around in django.db.models.options. You could try monkey patching DEFAULT_PARAMS, to allow you to set object_name in the model Meta class.

0

精彩评论

暂无评论...
验证码 换一张
取 消