Opened 6 years ago

Closed 6 years ago

#29961 closed Bug (fixed)

Change-Add-Delete links are visible in tabular inline for foreignkey fields with a HiddenInput widget

Reported by: Hidde Bultsma Owned by: Hidde Bultsma
Component: contrib.admin Version: 2.1
Severity: Normal Keywords: admin tabular inline
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

Setting the widget of an inline foreignkey field to HiddenInput in a TabularInline does not hide the change-add-delete links in the form. This bug does not appear when using a StackInline model admin.

Code sample:

models.py:

from django.conf import settings
from django.db import models

class Parent(models.Model):
    name = models.CharField(max_length=255)

class Child(models.Model):
    parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
    name = models.CharField(max_length=255)
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

admin.py:

from django.contrib import admin
from django.forms import widgets
from .models import Parent, Child

class ChildInline(admin.TabularInline):
    model = Child

    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == 'user':
            kwargs['initial'] = request.user.id
            kwargs['widget'] = widgets.HiddenInput
        return super().formfield_for_foreignkey(db_field, request, **kwargs)

@admin.register(Parent)
class ParentAdmin(admin.ModelAdmin):
    inlines = [ChildInline]

Steps to reproduce the bug:

  1. Open the Django admin.
  2. Add or change a parent. The change-add-delete links of the user field in the tabular inline are visible and glitched at the start of each row:

https://i.imgur.com/0eHQgmt.png

Change History (7)

comment:1 by Hidde Bultsma, 6 years ago

Owner: changed from nobody to Hidde Bultsma

comment:2 by Claude Paroz, 6 years ago

Are you able to determine if this is a regression (maybe by bisecting)?

in reply to:  2 comment:3 by Hidde Bultsma, 6 years ago

Replying to Claude Paroz:

Are you able to determine if this is a regression (maybe by bisecting)?

I will take a look at it.

comment:4 by Tim Graham, 6 years ago

Triage Stage: UnreviewedAccepted

I observed the same behavior as far back as I checked (Django 1.8), so if it's a regression it's not a recent one. The extra links come from RelatedFieldWidgetWrapper.

comment:5 by Hidde Bultsma, 6 years ago

Yeah, it looks like it has been there for at least since the admin was re-factored in 1.0.

comment:6 by Hidde Bultsma, 6 years ago

Has patch: set

comment:7 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 89a2216:

Fixed #29961 -- Made RelatedFieldWidgetWrapper hide related item links if wrapping a hidden widget.

Note: See TracTickets for help on using tickets.
Back to Top