#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:  [EMAIL PROTECTED]  |       Owner:  nobody    
   Status:  new                  |   Milestone:            
Component:  Forms                |     Version:  1.0       
 Keywords:                       |       Stage:  Unreviewed
Has_patch:  0                    |  
---------------------------------+------------------------------------------
 I have a custom Form for one of my models (in the example !SecondModel)
 that adds one choice field with an initial value.
 If I use that model/form as an inline formset and I exclude the extra
 field using the "fields" attribute of the !ModelAdmin, I got "This Field
 is required" on all the forms of the formset, including those who where
 left blank. Here a simple example:
 {{{
 #!python
 # models.py
 from django.db import models

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

 class SecondModel(models.Model):
     link = models.ForeignKey(FirstModel)
     c = models.CharField(max_length=10)

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

 class SecondAdmin(admin.TabularInline):
     model = models.SecondModel
     form = forms.SecondForm
     fields = ['c']
     extra = 3

 class FirstAdmin(admin.ModelAdmin):
     inlines = [SecondAdmin]

 admin.site.register(models.FirstModel, FirstAdmin)

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

 DUMMY_CHOICES = (
     (0, 'a'),
     (1, 'b'),
     (2, 'c')
 )

 class SecondForm(forms.ModelForm):
     d = forms.IntegerField(label='d', initial=0,
 widget=forms.Select(choices=DUMMY_CHOICES))
     class Meta:
         model = models.SecondModel
 }}}

 The problem is {{{fields = ['c']}}}, commenting out that line I get no
 errors.
 I guess, this is beacuse of the {{{has_changed}}} check, called by the
 {{{full_clean}}} method of {{{forms.Form}}}. It happens that
 {{{changed_data}}} compares the initial "d" value that had been set to 0
 with the result of "field.widget.value_from_datadict(self.data,..." that
 is None because we have not a key for "d" in self.data. So changed_data
 will contain "d" even if the form has not been changed.

 From django/forms/forms.py
 {{{
 #!python
                 prefixed_name = self.add_prefix(name)
                 data_value = field.widget.value_from_datadict(self.data,
 self.files, prefixed_name)
                 if not field.show_hidden_initial:
                     initial_value = self.initial.get(name, field.initial)

 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/9373>
Django <http://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 post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to