#7975: Admin inlines incorrectly report "this field is required" if inline model
has field with default value
--------------------------------------+-------------------------------------
          Reporter:  russellm         |         Owner:  msaelices
            Status:  reopened         |     Milestone:  1.0      
         Component:  Admin interface  |       Version:  SVN      
        Resolution:                   |      Keywords:           
             Stage:  Accepted         |     Has_patch:  0        
        Needs_docs:  0                |   Needs_tests:  0        
Needs_better_patch:  0                |  
--------------------------------------+-------------------------------------
Comment (by msaelices):

 @kmtracey, ok, now I see the bug, and I understand all.

 @jacob, @malcolm, I'm looking for your proposal of hidden fields, but it
 is very difficult to fix this without changing a lot of code, because:

  * model fields knows about callable default.
  * forms has no idea about callable defaults.
  * widget {{{_has_changed}}} method doesn't know about other values than
 {{{initial}}} and {{{data_value}}}, and has no access to form field.

 I think the most "success" approach may be this:

 {{{formfield}}} method in model field, who knows that {{{default}}} is a
 callable, could create a form field with a new optional parameter (i.e.
 {{{hidden_initial=True}}}). Then, inside field constructor, we create a
 default widget passing same {{{hidden_initial}}} (another optional
 parameter we must to add).

 But we still has problems. Look at a fragment code like this:
 {{{
 #!python
 field = forms.CharField(initial='hello', hidden_initial=True)
 field.widget = MyCustomWidget()
 }}}

 In last code, {{{CharField.__init__}}} will create a widget with
 {{{hidden_initial}}}, but after that we replace the widget again. Widget
 don't know about field, and this implies that {{{render}}} method will
 never add a {{{<input type="hidden" ... />}}}. This can be workaround with
 a property in base {{{Field}}} class, with a setter.

 Now, next step was modify {{{_get_changed_data}}} method in {{{BaseForm}}}
 class. The code is:
 {{{
 #!python
     def _get_changed_data(self):
         if self._changed_data is None:
             self._changed_data = []
             for name, field in self.fields.items():
                 prefixed_name = self.add_prefix(name)
                 data_value = field.widget.value_from_datadict(self.data,
 self.files, prefixed_name)
                 initial_value = self.initial.get(name, field.initial)
                 if field.widget._has_changed(initial_value, data_value):
                     self._changed_data.append(name)
         return self._changed_data
     changed_data = property(_get_changed_data)
 }}}

 There are two approachs here:

  1. Change {{{value_from_datadict}}}, to maybe return a {{{NOT_CHANGED}}}
 marker (or something like that), and then {{{_has_changed}}} returns
 {{{False}}}. I don't like this much.
  1. Change {{{_get_changed_data}}} itself to do logic. Something like:
 {{{
 #!python
    data_value = field.widget.value_from_datadict(self.data, self.files,
 prefixed_name)
    if not field.hidden_initial:
        initial_value = self.initial.get(name, field.initial)
    else:
        initial_value = self.initial.get(name,
 field.widget.hidden_initial_from_datadict(self.data, self.files,
 prefixed_name)
    if field.widget._has_changed(initial_value, data_value):
        ...
 }}}

 I don't know if I'm in right direction. Jacob, Malcolm, could you help me?

-- 
Ticket URL: <http://code.djangoproject.com/ticket/7975#comment:9>
Django Code <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