#23501: remove/add fields programatically in modelform fails in admin -------------------------------+-------------------- Reporter: hadisunyoto | Owner: nobody Type: Bug | Status: new Component: contrib.admin | Version: 1.7 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------+-------------------- 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 -- Ticket URL: <https://code.djangoproject.com/ticket/23501> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/054.fd1b0d315fa9ff9aa3e0529dc43ac1cc%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.