#9373: "This field is required" error even on empty inlines formsets in the 
admin
model page, when hiding a choice field of a custom form.
--------------------------------+---------------------------------------
     Reporter:  tyrion.mx@…     |                    Owner:  schacki
         Type:  Bug             |                   Status:  assigned
    Component:  Forms           |                  Version:  1.7-alpha-1
     Severity:  Normal          |               Resolution:
     Keywords:  admin, inlines  |             Triage Stage:  Accepted
    Has patch:  0               |      Needs documentation:  0
  Needs tests:  0               |  Patch needs improvement:  0
Easy pickings:  0               |                    UI/UX:  0
--------------------------------+---------------------------------------

Comment (by anonymous):

 I could verify this issue for 1.7-beta. However, the root cause is not
 related to Inlines. So let me set up a new example to explain in more
 detail:

 {{{
 # models.py
 from django.db import models

 class MyModel(models.Model):
     a = models.CharField(max_length=10)



 # forms.py
 from django import forms
 from . import models

 DUMMY_CHOICES = (
     (0, 'x'),
     (1, 'y'),
     (2, 'z')
 )

 class MyForm(forms.ModelForm):
     b = forms.IntegerField(label='d', initial=0,
 widget=forms.Select(choices=DUMMY_CHOICES))
     class Meta:
         model = models.MyModel

 # admin.py
 from django.contrib import admin
 from . import models, forms

 class MyAdmin(admin.ModelAdmin):
     model = models.MyModel
     form = forms.MyForm
     fields = ['a']

 admin.site.register(models.MyModel, MyAdmin)
 }}}


 '''1. MyForm'''
 The resulting form MyForm would have the form fields 'a' and 'b'. This
 behavior is documented and supposed to be like that.

 '''2. MyAdmin Form'''
 MyAdmin has the form attribute. This form contains the fields 'a' and 'b'.
 At the same time it has the fields attribute, which only refers to 'a'.
 This seemingly contradictory information is resolved as follows: the
 AdminModel calls the modelform_factory with form=MyForm and fields=['a'].
 The modelform_ factory works like a normal ModelForm described in 1. I.e.
 the resulting form contains the the fields 'a' and 'b'

 '''3. MyAdmin rendering'''
 However, for rendering, the ModelAdmin refers either to its field or
 fieldsets attribute. Thus, the field 'b' will not be rendered, also it is
 available in the form.

 '''4. MyAdmin Form Validation'''
 For any kind of validation, the generated form with fields 'a' and 'b' is
 used, also only field 'a' was available in the rendered form. This
 conflict has generated the issue, that has been described in the ticket.


 Bottom line: the behavior is as implemented (possibly as expected by the
 experts) and there are numerous situations, where it is required, e.g.
 UserAdmin. Nevertheless, this behavior is not expected and very hard to
 anticipate, especially by beginners. And debugging is very, very
 difficult, since it reveals itself only very late in the form handling
 process.


 Possible resolutions:

 '''A. Won't Fix'''
  A "won't fix" is therefore not appropriate.

 '''B. Raise Warning'''
 It could be checked during the ModelAdmin instantiation, if we have less
 fields for display than we we have in the form and raise a warning.
 However, this would annoy people, who set it up like that on purpose.

 '''C. Raise Error'''
 Like B, but raise an error; wich backwards incompatible and probably not
 worth the effort

 '''D. Implement a Check'''
 A check (as part of the 1.7 application check), could identify such
 situations and raise warnings. I am not sure if and how this would be
 possible (never done it).

 '''E. Amending fields/fieldsets/exclude'''
 If a check identifies the mismatch between admin fields and formfields, we
 could:
 a) add the missing field(s) to the admin fields list
 b) add the missing field(s) to the admin formfields; however this could
 only be a plug, since it is not clear, where to add it in the fieldsets
 c) add the missing field(s) to the admin exclude list, since exclude takes
 precedence.
 All 3 options might be backwards incompatible

 '''F. Amending form generation'''
 If a check identifies the mismatch between admin fields and formfields, we
 could adapt the provided form such that the critical field(s) is removed
 from the form. This might be backwards incompatible.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/9373#comment:7>
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/077.0523eef4b3c01c7ec5b89908a7db9f67%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to