Opened 7 years ago

Closed 3 years ago

#11277 closed Bug (fixed)

Hidden fields in Inlines are displayed as empty rows

Reported by: Martin Mahner Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords: admin inlines hiddeninput sprint2013
Cc: thepapermen, chrismedrela, riccardo.magliocchetti@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: yes

Description

A field with an Hiddeninput widget is displayed as an empty row in the inline in admin. See screenshot for explanation.

Here is the code I use:

class LocationForm(forms.ModelForm):
    latitude = forms.FloatField(widget=forms.HiddenInput)
    longitude = forms.FloatField(widget=forms.HiddenInput)

class LocationInline(generic.GenericStackedInline):
    model = Location
    form = LocationForm
    extra = 1
    max_num = 1

Attachments (4)

screenshot_inline_empty.png (14.2 KB) - added by Martin Mahner 7 years ago.
11277-admin-hiddeninput-helpers.diff (830 bytes) - added by Ben Boyd <beathan@…> 5 years ago.
11277-admin-hiddeninput-fieldset.html.diff (1002 bytes) - added by Ben Boyd <beathan@…> 5 years ago.
testproject.tgz (7.8 KB) - added by Pieter Swinkels 5 years ago.
test project to reproduce the reported problem

Download all attachments as: .zip

Change History (25)

Changed 7 years ago by Martin Mahner

Attachment: screenshot_inline_empty.png added

comment:1 Changed 7 years ago by Alex Gaynor

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

a) What fields on are Location

b) What do you expect to happen?

comment:2 Changed 7 years ago by Alex Gaynor

Resolution: worksforme
Status: newclosed

No new information after 2 months, closing as worksforme.

comment:3 Changed 6 years ago by Narcélio Filho

Resolution: worksforme
Status: closedreopened

The issue is that labels are still being rendered on forms, even when the field is hidden. Labels should not be rendered if field is hidden, right?

(Maybe the problem is at django/contrib/admin/templates/admin/includes/fieldset.html or AdminField.label_tag().)

Checkout a discussion about that:

How do I hide the field label for a HiddenInput widget in Django Admin?

comment:4 Changed 6 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

comment:5 Changed 6 years ago by Julien Phalip

#14372 was closed as dupe and contains a patch.

comment:6 Changed 5 years ago by Julien Phalip

Severity: Normal
Type: Bug

Changed 5 years ago by Ben Boyd <beathan@…>

Changed 5 years ago by Ben Boyd <beathan@…>

comment:7 Changed 5 years ago by Ben Boyd <beathan@…>

Easy pickings: unset

I've attached 2 diffs as a proposed patch for this issue, tested on the latest trunk. One adds has_hidden_field to Fieldline in django/contrib/admin/helpers.py, marking it True if self.fields contains a field with a hidden widget, and another for django/contrib/admin/templates/admin/includes/fieldset.html, which outputs only the fields themselves if line.has_hidden_field is True.

comment:8 Changed 5 years ago by Ben Boyd <beathan@…>

Easy pickings: set

comment:9 Changed 5 years ago by Pieter Swinkels

UI/UX: unset

DjangoCon Europe 2011 sprint: commenced work on this ticket

comment:10 Changed 5 years ago by Julien Phalip

UI/UX: set

Changed 5 years ago by Pieter Swinkels

Attachment: testproject.tgz added

test project to reproduce the reported problem

comment:10 Changed 5 years ago by Pieter Swinkels

UI/UX: unset

I tested it with the project I just attached but I cannot reproduce the problem.

comment:11 Changed 5 years ago by beathan@…

Has patch: set

comment:12 Changed 5 years ago by Julien Phalip

Easy pickings: unset
Needs tests: set
Patch needs improvement: set
UI/UX: set

The patch needs tests. It also needs to cater for when the Fieldline contains only one, hidden, field (in which case the entire row, including the label and help_text, show be hidden -- see #7859 and #14372), or when it contains a mix of hidden and non-hidden fields (in which case the row should be displayed and the non-hidden fields should also be displayed correctly).

By the way, I could reproduce the issue with the following code:

# models.py

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Restaurant(models.Model):
    name = models.CharField(max_length=200)

class Location(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = generic.GenericForeignKey('content_type', 'object_id')
    longitude = models.FloatField(blank=True)
    latitude = models.FloatField(blank=True)

# admin.py

from django.contrib import admin
from django import forms
from django.contrib.contenttypes.generic import GenericStackedInline

from .models import Restaurant, Location

class LocationForm(forms.ModelForm):
    latitude = forms.FloatField(widget=forms.HiddenInput)
    longitude = forms.FloatField(widget=forms.HiddenInput)

class LocationInline(GenericStackedInline):
    model = Location
    form = LocationForm
    extra = 1
    max_num = 1

class RestaurantAdmin(admin.ModelAdmin):
    inlines = [LocationInline]

admin.site.register(Restaurant, RestaurantAdmin)

This is however not tied to generic inlines in particular, but is relevant to any form of any kind (main forms, regular inline forms and generic inline forms).

comment:13 Changed 5 years ago by thepapermen

Cc: thepapermen added

comment:14 Changed 4 years ago by Radosław Sieradzki

Keywords: sprint2013 added

comment:15 Changed 4 years ago by chrismedrela

Cc: chrismedrela added
Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

Pull request: https://github.com/django/django/pull/789. Reviewed by rsiera.

comment:16 Changed 4 years ago by rm_

Cc: riccardo.magliocchetti@… added

comment:17 Changed 4 years ago by Aymeric Augustin

Status: reopenednew

comment:18 Changed 3 years ago by Tim Graham

Patch needs improvement: set
Triage Stage: Ready for checkinAccepted

Patch no longer applies cleanly.

comment:19 Changed 3 years ago by Russell Keith-Magee

Pull request #1260 was provided as a fix for this ticket; still not RFC, though. Comments on the pull request.

comment:20 Changed 3 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In dc3d2ac98c1bcfad74d3e9523caf07e7e9fb15aa:

Fixed #11277 -- Hid labels of fields with HiddenInput widget in admin forms.

Thanks bartTC for the report.

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