Jon
Jon

Reputation: 3194

Django Admin: How to display more fields as links?

The default admin page for Django automatically makes the first heading of each table a link to edit the information (see below):

enter image description here

Clicking the ID column data will take you to a page to edit the fields in the selected table, in this case Applicants

Is there a way of changing this setup, so that Surname is the link to edit and not ID?

Many Thanks.

Upvotes: 4

Views: 2105

Answers (3)

Be careful, if 'surname' is not assigned to list_display but assigned to list_display_links as shown below:

class ApplicantsAdmin(admin.ModelAdmin):
    list_display = (
        'id', 
        'first_name', 
        # 'surname', # Here
        'location', 
        'job'
    )                     # Here
    list_display_links = ('surname',)

You will get the error below:

ERRORS: <class 'xxx.admin.Applicant'>: (admin.E111) The value of 'list_display_links[2]' refers to 'surname', which is not defined in 'list_display'.

So, 'surname' must be assigned to both list_display and list_display_links as shown below:

class ApplicantsAdmin(admin.ModelAdmin):
    list_display = (
        'id', 
        'first_name', 
        'surname', # Here
        'location', 
        'job'
    )                     # Here
    list_display_links = ('surname',)

Upvotes: 1

rnevius
rnevius

Reputation: 27092

Use list_displays_links to control if and which fields in list_display should be linked to the edit page for an object.

Example usage:

class PersonAdmin(admin.ModelAdmin):
    list_display = ('first_name', 'surname', 'location')
    list_display_links = ('first_name', 'surname')

...would make both first_name and surname clickable.

Upvotes: 5

Hasan Ramezani
Hasan Ramezani

Reputation: 5194

based on document:

By default, the change list page will link the first column – the first field specified in list_display – to the change page for each item. But list_display_links lets you change this:

  • Set it to None to get no links at all.

  • Set it to a list or tuple of fields (in the same format as list_display) whose columns you want converted to links.

class ApplicantsAdmin(admin.ModelAdmin):
    list_display = ('id', 'first_name', 'surname', 'location', 'job') #fields that you want to display
    list_display_links = ('surname',)

you should select list_display_links from fields of list_display tuple.

Upvotes: 2

Related Questions