Opened 17 years ago
Closed 14 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: | dev |
| 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)
Change History (15)
comment:1 by , 17 years ago
| milestone: | → 1.0 |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
by , 17 years ago
| Attachment: | r8520-missing-exclude.patch added |
|---|
comment:2 by , 17 years ago
comment:3 by , 17 years ago
| Has patch: | set |
|---|
by , 17 years ago
| Attachment: | r8520-validate-username-2.patch added |
|---|
Fixed previous patch that wasn't checking for Meta (and tests failed)
comment:5 by , 17 years ago
comment:6 by , 17 years ago
| milestone: | 1.0 → post-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:8 by , 17 years ago
I can confirm this is still occurring in 1.0.2 - I cant seem to override the inline either
comment:9 by , 16 years ago
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 by , 16 years ago
comment:11 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Bug |
comment:12 by , 15 years ago
| 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 by , 14 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
| 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.
Above patch fixes this issue and automatically converts tuple or strings to lists, though I can't understand why you're defining the
excludetwice. The "correct" behaviour should be to setexcludeinIngredientsFormand then let the admin take care of it (actually theformsetoption isn't documented: should it be?).