Custom Django Adminsite Doesn't show link to read_only ForeignKey field

Hello Comunity. I am new in the Django Forum and this is my first question, but I have a year following this site.
This is my problem.

In the default Django Admin Site, if a model is registered with ForegingKey fields and those are included in readonly_fields (using the model property or the get_readonly_fields method) the value of the field is rendered with a link to the Change View, but this Doesn’t work on a custom Django Admin Site . E.g.: I have this two models:

class ModelA(models.Model):
    field_a = models.CharField(max_length=50)

class ModelB(models.Model):
    model_a = models.ForeignKey(ModelA, on_delete=models.PROTECT)

I registered in the default Django Admin Site:


class ModelBAdmin(admin.ModelAdmin):
    model = ModelB
    readonly_fields = ["model_a"]

So I get in the Change View of ModelB in the Admin the value of the field ( str of the model) with a link to the Change View of the related model:


But if I register the models in a Custom Admin Site, the link is not generated.

How can I extends my Custom Admin Site in order to generate those links?

PD1: I know I can code custom methods to build the links, but this is not a DRY way of do it and doesn’t work well with get_readonly_fields method

PD2: If I register the related model (ModelA in the example) in the default Admin Site the link is generated, but point to the default Admin Site, brokening the purpose of the Custom Admin Site.

PD3: Question posted in StackOverflow: python - Custom Django Adminsite Doesn't show link to read_only ForeignKey field - Stack Overflow

What precisely do you mean by a “Custom Admin Site”?

Hello @KenWhitesell, thanks for ask.

What I mean is that I have instantiated the class AdminSite from the package Admin, so I have two Admin Sites, the default and my custom AdminSite. I have followed the all the documantation about it: but I don’t understand why the URL of the link of the ForeignKey fields that are in “readonly_fields” in my ModelAdmin point to the default admin and not to the custom one. All other URL’s work perfectly, Index, Change View, ListView, etc. Even the endpoints of autocomplete fields works well pointing to the URL pattern of my Custom Admin.

Nobody? I really need help with this, and I think the question is explained. If someone needs more details of course I will provide them.

You have explained it well. For what it’s worth, I can recreate the issue, and I see (in general) where it’s coming from. I haven’t quite determined yet what an appropriate solution might be.

I have created a ticket for this: #33077 (Wrong URL generated by get_admin_url for readonly field in custom Admin Site) – Django

Thanks A lot @KenWhitesell .

I have been reading the source code, but I am not able to find the issue or where I can hack the Admin.

The details (including the file and function within the file) are in the details of the ticket.

Thanks a lot @KenWhitesell .
I have added the line an it works perfectly, It will works until the patch arrives.