Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change `object_name` of a model in Django

Tags:

python

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?

like image 666
Ram Rachum Avatar asked Oct 08 '11 00:10

Ram Rachum


1 Answers

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.

like image 180
Alasdair Avatar answered Oct 19 '22 17:10

Alasdair