Opened 8 years ago

Closed 5 years ago

#8071 closed Bug (duplicate)

Admin app ignores custom form settings for inline formsets for inline-edited foreign key models

Reported by: dan Owned by: nobody
Component: contrib.admin Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

custom form values appear to be being ignored for inline form sets in the admin app. Say if i try to eclude a field from an inline-edited form

consider a simple app: (in my case the app is called simple, in the project reducer

class Salad (models.Model) :
    name = models.TextField(max_length=100)
    
class Ingredient (models.Model) :
    name = models.TextField(max_length=100)
    salad = models.ForeignKey(Salad)
    unwanted = models.TextField(max_length=200)

If i define an admin site which will allow Ingredient to be edited inline in the Salad admin, then it appears i lose the ability to customize the form. say I wish to exclude the field "unwanted" from being edited in the inline formset. I defined an admin site thusly in the app's admin.py:

import reducer.simple.models as simplemodels 
from django.contrib import admin
from django import forms
from django.forms.models import inlineformset_factory

class IngredientForm(forms.ModelForm):
    class Meta:
        exclude = ("unwanted")
        model = simplemodels.Ingredient

IngredientInlineFormSet = inlineformset_factory(
  simplemodels.Salad, simplemodels.Ingredient,
  exclude = ["unwanted"],
  form = IngredientForm,
) 

class IngredientInline(admin.TabularInline): 
    model =  simplemodels.Ingredient
    extra = 3
    formset = IngredientInlineFormSet
    form = IngredientForm

class SaladAdmin(admin.ModelAdmin):
    inlines = ( IngredientInline, )

adminsite = admin.AdminSite()
adminsite.register(simplemodels.Salad, SaladAdmin)

Now if i fire this site up in a development server and browse to http://localhost:8000/admin/simple/salad/add/, i will find that the "unwanted" field is still available in the admin interface, despite being excluded from the form that i have specified as the admin

there is some redundancy in my test case above; I have specified the from both in IngredientInline and IngredientInlineFormSet; I have specified the excluded fields both in IngredientForm and in IngredientInlineFormSet. However, I have made still further reduced cases where these values are specified once each and they still fail.

It's the end of a long day and I'll be away for the weekend, so I'm posting this issue here now rather than tracing any further down through the newforms admin site code, whose mysterious form rendering logic i have yet to grok. Hopefully someone wiser than I will have a look.

Attachments (2)

r8520-missing-exclude.patch (1.0 KB) - added by Ivan Giuliani 8 years ago.
r8520-validate-username-2.patch (1.0 KB) - added by Ivan Giuliani 8 years ago.
Fixed previous patch that wasn't checking for Meta (and tests failed)

Download all attachments as: .zip

Change History (15)

comment:1 Changed 8 years ago by Jacob

milestone: 1.0
Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset
Triage Stage: UnreviewedAccepted

Changed 8 years ago by Ivan Giuliani

Attachment: r8520-missing-exclude.patch added

comment:2 Changed 8 years ago by Ivan Giuliani

Above patch fixes this issue and automatically converts tuple or strings to lists, though I can't understand why you're defining the exclude twice. The "correct" behaviour should be to set exclude in IngredientsForm and then let the admin take care of it (actually the formset option isn't documented: should it be?).

comment:3 Changed 8 years ago by Ivan Giuliani

Has patch: set

Changed 8 years ago by Ivan Giuliani

Fixed previous patch that wasn't checking for Meta (and tests failed)

comment:4 Changed 8 years ago by Ivan Giuliani

(the patch is right, just confused the renaming... damn autocompletion)

comment:5 Changed 8 years ago by Brian Rosner

(In [8861]) Fixed #7973 -- Added exclude to BaseModelAdmin to make everything consistent with the form/formset factories. Refs #8071 to make it easier to get at exclude. Thanks julien for the patch.

comment:6 Changed 8 years ago by Brian Rosner

milestone: 1.0post-1.0

There is much larger problem here that is best solved all at once. However, there just isn't enough time for this before 1.0 Bumping to post-1.0. I committed [8861] to make it simpler to get at exclude so it is best to use that in the interim until a proper fix is in place.

comment:7 Changed 8 years ago by (none)

milestone: post-1.0

Milestone post-1.0 deleted

comment:8 Changed 7 years ago by onidaito

I can confirm this is still occurring in 1.0.2 - I cant seem to override the inline either

comment:9 Changed 7 years ago by Ramiro Morales

After r10619 (in trunk, r10620 in 1.0.x post 1.0.2), Meta options contained in a ModelForm (e.g. exclude in IngredientForm.Meta above) specified as the form option to an Inline are taken in account, so that half of what this ticket reported is fixed.

The exclude option to inlineformset_factory is still being ignored, though.

See also #8160.

comment:10 Changed 7 years ago by mariarchi

Shouldn't this be marked as a duplicate of #8160? After all, admin uses inlineformset_factory which is just a syntactic sugar around modelformset_factory, so fixing #8160 will automatically fix this issue as well.

comment:11 Changed 5 years ago by Luke Plant

Severity: Normal
Type: Bug

comment:12 Changed 5 years ago by Julien Phalip

Easy pickings: unset
Needs tests: set

At the very least this would need tests. But I agree it seems that #8160 is technically the same issue. It'd be good to address both tickets at once.

comment:13 Changed 5 years ago by Claude Paroz

Resolution: duplicate
Status: newclosed
UI/UX: unset

This works on current trunk. I'm marking it as a duplicate of #8160 for which I will upload a test case.

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