Django "xxxxxx Object" display customization in admin action sidebar
I would like to change the default behavior of how the admin recent changes sidebar displays the name of "objects" added. Refer to the picture below:
I would like to change how these are named in the Admin. Ideally, I would like to be able to change it from "MyModelName object" to, as in the "Policy" object example, something like "Policy: {{ value of the policy's "Policy Name" field. }}.
I was thinking that __unicode__
for my Patient model handled this, but it doesn't appear to. Any assistance is appreciated.
Solution 1:
__unicode__
does do that. Your model should look something like this:
class SomeModel(models.Model):
def __unicode__(self):
return 'Policy: ' + self.name
On Python 3 you need to use __str__
:
def __str__(self):
return 'Policy: ' + self.name
Solution 2:
Using the __str__
method works on Python3 and Django1.8:
class MyModel(models.Model):
name = models.CharField(max_length=60)
def __str__(self):
return 'MyModel: {}'.format(self.name)
Solution 3:
The string you're seeing is coming from __unicode__
method, as others have mentioned. But the thing is that admin saves string representation of an object when it creates log event, therefore if you add __unicode__
implementation after the log entry was saved, you won't see new titles on old items, only after you make some new activity