4

I inserted "creator"-method to my model like this:

def creator(self):
        return self.user

Then i'd add this line to Admin-class of this model^

list_display = ('title','created_at','votes','creator')

And there was all good... usernames are displayed on that column, but i want to make links from that names which will forward me to edit that user-profiles. How can i do this? Thanks a lot!

korovaisdead
  • 6,271
  • 4
  • 26
  • 33

3 Answers3

4

First, add 'user_link' to list_display. Then, add this to your ModelAdmin:

def user_link(self, obj):
        return '<a href="%s">%s</a>' % (
            urlresolvers.reverse('admin:auth_user_change', args=(obj.user.id,)), obj.user
            )
user_link.allow_tags = True
user_link.short_description = 'User'

(untested)

schneck
  • 10,556
  • 11
  • 49
  • 74
0

There is a better alternative: raw_id_fields.

@admin.register(Ticket)
class AdminTicket(admin.ModelAdmin):
    fields = ['user', 'subject', 'message']
    raw_id_fields = ['user']

Here is how this will look like:

enter image description here

Max Malysh
  • 29,384
  • 19
  • 111
  • 115
0

In Django 2.0 you cannot use allow_tags = True anymore. Instead, you need to mark the string as safe by returning mark_safe("<a href="...">...</a>).