Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 665711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:44:19+00:00 2026-05-13T23:44:19+00:00

If a django model contains a foreign key field, and if that field is

  • 0

If a django model contains a foreign key field, and if that field is shown in list mode, then it shows up as text, instead of displaying a link to the foreign object.

Is it possible to automatically display all foreign keys as links instead of flat text?

(of course it is possible to do that on a field by field basis, but is there a general method?)

Example:

class Author(models.Model):
    ...

class Post(models.Model):
    author = models.ForeignKey(Author)

Now I choose a ModelAdmin such that the author shows up in list mode:

class PostAdmin(admin.ModelAdmin):
    list_display = [..., 'author',...]

Now in list mode, the author field will just use the __unicode__ method of the Author class to display the author. On the top of that I would like a link pointing to the url of the corresponding author in the admin site. Is that possible?

Manual method:

For the sake of completeness, I add the manual method. It would be to add a method author_link in the PostAdmin class:

def author_link(self, item):
    return '<a href="../some/path/%d">%s</a>' % (item.id, unicode(item))
author_link.allow_tags = True

That will work for that particular field but that is not what I want. I want a general method to achieve the same effect. (One of the problems is how to figure out automatically the path to an object in the django admin site.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T23:44:20+00:00Added an answer on May 13, 2026 at 11:44 pm

    I don’t think there is a mechanism to do what you want automatically out of the box.

    But as far as determining the path to an admin edit page based on the id of an object, all you need are two pieces of information:

    a) self.model._meta.app_label

    b) self.model._meta.module_name

    Then, for instance, to go to the edit page for that model you would do:

    '../%s_%s_change/%d' % (self.model._meta.app_label, self.model._meta.module_name, item.id)
    

    Take a look at django.contrib.admin.options.ModelAdmin.get_urls to see how they do it.

    I suppose you could have a callable that takes a model name and an id, creates a model of the specified type just to get the label and name (no need to hit the database) and generates the URL a above.

    But are you sure you can’t get by using inlines? It would make for a better user interface to have all the related components in one page…

    Edit:

    Inlines (linked to docs) allow an admin interface to display a parent-child relationship in one page instead of breaking it into two.

    In the Post/Author example you provided, using inlines would mean that the page for editing Posts would also display an inline form for adding/editing/removing Authors. Much more natural to the end user.

    What you can do in your admin list view is create a callable in the Post model that will render a comma separated list of Authors. So you will have your Post list view showing the proper Authors, and you edit the Authors associated to a Post directly in the Post admin interface.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django model that contains a date field: class Event(models.Model): event_date =
I have a text field in my model that contains markdown text. I need
I have a Django model which has a foreign key that is optional. I
I have a model for categories, which contains a circular foreign key. I dumped
I've got a django model that contains a manytomany relationship, of the type, class
I have a problem with Django queryset ordering. My model contains a field named
I have a django model with a Charfield that contains the unicode escaped string
I have a Django model containing a text field. In the admin GUI, I'd
How to specify custom constraint for Django model? Like that create table t (
If I change a field in a Django model, how can I synchronize it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.