Django “xxxxxx Object” display customization in admin action sidebar

前端 未结 9 1045
野的像风
野的像风 2020-12-02 10:07

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:

9条回答
  •  一向
    一向 (楼主)
    2020-12-02 10:57

    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.

提交回复
热议问题