128

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:

In the recent actions module, it also shows new objects as "MyModelName object"

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.

2
  • 1
    __unicode__ does do that. Paste your code so we can see what's wrong Feb 17, 2012 at 22:50
  • __unicode__ did not work for me in Python 3. However, __str__ did work as documented in the answer below
    – Dan King
    Apr 5, 2019 at 17:17

9 Answers 9

191

__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
2
  • 1
    I foolishly didn't notice that I'd declared unicode methods for every model EXCEPT this one. :doh: Sorry all.
    – patrickn
    Feb 17, 2012 at 23:04
  • 2
    @patrickn, thanks for this foolish decision you made 9 years ago. :) Aug 11, 2021 at 21:26
31

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)
2
  • 3
    This solved for me as well. I also use Python3 and Django1.8. Nov 25, 2015 at 21:45
  • str is the old way.....yet python 3 with Django, the new way (unicode) does not work?
    – user798719
    Jan 23, 2018 at 17:21
14

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

2
  • I was just wondering why the Actions log wasn't showing up the names after I set unicode - I should of known it was just a log stored in the db taking data at the time of creation! Sep 11, 2012 at 10:45
  • 2
    thank you, would've given me a big hassle if you hadn't explained that
    – dtc
    Feb 1, 2013 at 1:29
7

The answers mentioning __str__ and __unicode__ methods are correct. As stated in the docs however, since version 1.6 (I think), you can use the python_2_unicode_compatible decorator for both Python 2 and Python 3:

from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class MyClass(models.Model):
    def __str__(self):
        return "Instance of my class"

You can use the above in non-Model objects as well.

6

This would work, using def str(self): which returns self.title

Use something like:

class Blog(models.Model):
    title = models.CharField(max_length=200)
    def __str__(self):
        return self.title
4

You need to define, which column that you want to display...

for example:

class POAdmin(admin.ModelAdmin):
    list_display = ('qty', 'cost', 'total')
2

You're right in thinking that __unicode__ does that. I have this running right now:

class Film(models.Model):
    title = models.CharField(max_length=200)
    ...
    def __unicode__(self):
        return self.title

When I look in the recent actions list, I see the title of the film that I have just edited.

2

By adding __str__() method to the model Patient this way:

class Patient(models.Model):
name=models.CharField(max_length=200)
#.........
def __str__(self):
    return self.name

will display name of patient instead object. For detail check here

1

Since this question is 6 years old, a lot of things have changed. Let me make an update to it.With python3.6 and the latest version of Django (2.1.2) you should always use __str__() in new code. __unicode__() is an old story for python2.7 because in python3, str is unicode.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.