[Django]-How to override models __str__ method so you would have different presentation in admin panel

4👍

Instead of doing it in __str__ method, you can specify which fields to display in the list in admin.

class MyModelAdmin(admin.ModelAdmin):
    def get_list_display(self, request):
        if request.user.is_superuser:
            return ('title' , 'created_by')
        else:
            return ('title',)
admin.site.register(MyModel, MyModelAdmin)

and then update your __str__ method to return just the title.

👤v1k45

Leave a comment