#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         |               Resolution:
     Keywords:                 |             Triage Stage:  Unreviewed
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+--------------------------------------
Changes (by hadisunyoto):

 * needs_better_patch:   => 0
 * needs_tests:   => 0
 * needs_docs:   => 0


Old description:

> 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

New description:

 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 (created previously 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#comment:1>
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/069.10cbfb928ae43f6f0350aa532902f87c%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to