Django admin. Link to edit user profile from list_display

I introduced the “creator” method into my model as follows:

def creator(self): return self.user 

Then I will add this line to the Admin class of this model ^

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

And everything was fine ... usernames are displayed in this column, but I want to make links from these names that will redirect me to edit these user profiles. How can i do this? Thank you very much!

+4
source share
2 answers

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' 

(unverified)

+3
source

There is a better alternative: raw_id_fields .

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

Here's how it would look:

enter image description here

0
source

All Articles