Ram Rachum
Ram Rachum

Reputation: 88718

How to change `object_name` of a model in Django

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 object_name seems to be stored in the Options instance, but how do I change that?

Upvotes: 7

Views: 1464

Answers (1)

Alasdair
Alasdair

Reputation: 309109

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.

Upvotes: 2

Related Questions