﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23501	remove/add fields programatically in modelform fails in admin	hadisunyoto	nobody	"model:
{{{
class Thickness(models.Model):
    value = models.DecimalField(primary_key=True, max_digits=7, decimal_places=2)
    is_active = models.BooleanField()
}}}
model form:
{{{
class ThicknessForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(ThicknessForm, self).__init__(*args, **kwargs)
        instance = getattr(self, 'instance', None)

        # edit
        if instance and instance.pk:
            del self.fields['value']
        else:
            del self.fields['is_active']

    class Meta:
        model = Thickness
        fields = '__all__'
}}}

admin:
{{{
class ThicknessAdmin(admin.ModelAdmin):
    list_display = ('value', 'is_active')
    form = ThicknessForm

admin.site.register(Thickness, ThicknessAdmin)
}}}

refresh page, go to admin->Thickness, create one:

KeyError at /admin/thicknesses/thickness/add/
""Key 'is_active' not found in 'ThicknessForm'""
...

Error during template rendering

In template D:\env\lib\site-packages\django\contrib\admin\templates\admin\includes\fieldset.html, error at line 7

The highlighted line is number 7

6    {% for line in fieldset %}
7        <div class=""form-row{% if line.fields|length_is:'1' and line.errors %} errors{% endif %}{% if not line.has_visible_field %} hidden{% endif %}{% for field in line %}{% if field.field.name %} field-{{ field.field.name }}{% endif %}{% endfor %}"">
8            {% if line.fields|length_is:'1' %}{{ line.errors }}{% endif %}

go to admin->Thickness again, modify one (without using model form):
KeyError at /admin/thicknesses/thickness/0.55/
""Key 'value' not found in 'ThicknessForm'""
...

same error as above

Without admin, model form works as expected, it remove is_active when create, and it remove value when edit"	Bug	new	contrib.admin	1.7	Normal				Unreviewed	0	0	0	0	0	0
