Code

Opened 5 years ago

Closed 7 months ago

#11277 closed Bug (fixed)

Hidden fields in Inlines are displayed as empty rows

Reported by: bartTC 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 bartTC 5 years ago.
11277-admin-hiddeninput-helpers.diff (830 bytes) - added by Ben Boyd <beathan@…> 3 years ago.
11277-admin-hiddeninput-fieldset.html.diff (1002 bytes) - added by Ben Boyd <beathan@…> 3 years ago.
testproject.tgz (7.8 KB) - added by PieterSwinkels 3 years ago.
test project to reproduce the reported problem

Download all attachments as: .zip

Change History (25)

Changed 5 years ago by bartTC

comment:1 Changed 5 years ago by Alex

  • 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 5 years ago by Alex

  • Resolution set to worksforme
  • Status changed from new to closed

No new information after 2 months, closing as worksforme.

comment:3 Changed 4 years ago by narcelio

  • Resolution worksforme deleted
  • Status changed from closed to reopened

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 4 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

comment:5 Changed 3 years ago by julien

#14372 was closed as dupe and contains a patch.

comment:6 Changed 3 years ago by julien

  • Severity set to Normal
  • Type set to Bug

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

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

comment:7 Changed 3 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 3 years ago by Ben Boyd <beathan@…>

  • Easy pickings set

comment:9 Changed 3 years ago by PieterSwinkels

  • UI/UX unset

DjangoCon Europe 2011 sprint: commenced work on this ticket

comment:10 Changed 3 years ago by julien

  • UI/UX set

Changed 3 years ago by PieterSwinkels

test project to reproduce the reported problem

comment:10 Changed 3 years ago by PieterSwinkels

  • UI/UX unset

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

comment:11 Changed 3 years ago by beathan@…

  • Has patch set

comment:12 Changed 3 years ago by julien

  • 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 2 years ago by thepapermen

  • Cc thepapermen added

comment:14 Changed 14 months ago by rsiera

  • Keywords sprint2013 added

comment:15 Changed 14 months ago by chrismedrela

  • Cc chrismedrela added
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Accepted to Ready for checkin

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

comment:16 Changed 14 months ago by rm_

  • Cc riccardo.magliocchetti@… added

comment:17 Changed 13 months ago by aaugustin

  • Status changed from reopened to new

comment:18 Changed 11 months ago by timo

  • Patch needs improvement set
  • Triage Stage changed from Ready for checkin to Accepted

Patch no longer applies cleanly.

comment:19 Changed 10 months ago by russellm

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

comment:20 Changed 7 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In dc3d2ac98c1bcfad74d3e9523caf07e7e9fb15aa:

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

Thanks bartTC for the report.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.