Django

Code

Ticket #7975 (closed: fixed)

Opened 4 months ago

Last modified 3 months ago

Admin inlines incorrectly report "this field is required" if inline model has field with default value

Reported by: russellm Assigned to: brosner
Milestone: 1.0 Component: django.contrib.admin
Version: SVN Keywords: 1.0-blocker
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

Consider the following model:

class Person(models.Model):
    name = models.CharField(max_length=128)

class Membership(models.Model):
    person = models.ForeignKey(Person)
    date_joined = models.DateTimeField(default=datetime.now)

with the following admin definition:

class MembershipInline(admin.TabularInline):
    model = Membership
    extra = 3

class PersonAdmin(admin.ModelAdmin):
    inlines = (MembershipInline,)
    
admin.site.register(Person, PersonAdmin)

If you try to add or edit an instance of Person in the admin, you won't be able to save unless you fill in _all_ the extra lines in the inline.

This appears to be because the default value that is used to populate the inline form is confused with legitimate user-entered content. As a result, all the empty extra rows appear to have been partially filled out, so the validator raises an error for the 'missing' fields.

If you remove the default value from the date_joined field, the problem goes away.

Attachments

callable_initial_fix_r8640.diff (6.8 kB) - added by msaelices on 08/27/08 19:22:08.
Patch that fixes this extreme bug, but with no tests yet.
callable_initial_fix_r8640.2.diff (6.8 kB) - added by msaelices on 08/27/08 19:24:51.
Only a typo in a doc string
callable_initial_fix_with_tests_r8776.diff (11.6 kB) - added by msaelices on 08/31/08 19:55:40.
Patch with tests

Change History

07/26/08 09:33:41 changed by russellm

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

At the very least, this should be documented as a limitation for inlined models. Ideally, it shouldn't be raised as a validation error. The easiest solution I can think of is to ignore any field with a default value when establishing if a row has been edited. Brian or Joseph might have some better ideas.

07/26/08 10:47:55 changed by kmtracey

This has come up before and been fixed, though I'm not sure it was ever fixed for this specific case. I think the significant thing in your example is that your default is a callable (datetime.now) that is returning a different value when the form is submitted than it returned when the form was created. It's not a general problem with having defaults in the inlines, nor even with callable defaults (changing the example to use just a DateField and date.today() for the default works, assuming you don't allow the clock to tick past midnight while you stare at the unsubmitted form). (BTW you also need to add at least one more field to the Membership model to hit the error, as it is all the inlines appear to be fully complete with just the default DateTime filled in.)

08/08/08 12:57:05 changed by ericholscher

  • stage changed from Unreviewed to Design decision needed.

08/22/08 19:22:48 changed by jacob

Wow.

This one is awesome.

08/22/08 19:25:16 changed by jacob

  • stage changed from Design decision needed to Accepted.

After talking with Malcolm, I think the solution is to put a hidden field with the original value for anything with a callable default. A bit nasty, yes, but really the only way to fix this.

08/25/08 16:00:05 changed by msaelices

  • owner changed from nobody to msaelices.
  • status changed from new to assigned.

08/25/08 16:06:30 changed by msaelices

  • status changed from assigned to closed.
  • resolution set to worksforme.

I've testing this ticket, and the validation error doesn't appears. Instead of this, other strange thing occurs. All new rows are created, with default value. But this is a normal behaviour, because the confusion still there. This will be happend also in normal (not inline) forms.

I mark this as worksforme.

08/25/08 16:14:32 changed by kmtracey

  • status changed from closed to reopened.
  • resolution deleted.

Notice the parenthetical remark I added above:

(BTW you also need to add at least one more field to the Membership model to hit the error, as it is all the inlines appear to be fully complete with just the default DateTime filled in.)

That explains the behavior you describe. To hit the error message you need to have at least one more field in Membership model, otherwise the inlines all appear to be completely filled in and they are simply added. This problem still exists, it's just the original models provided don't (and never did) actually demonstrate the problem.

08/27/08 16:19:03 changed 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:

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:

    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.
  2. Change _get_changed_data itself to do logic. Something like:
       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?

08/27/08 16:21:50 changed by msaelices

From last comment, I will add, that maybe is not necessary to add hidden_initial to widget. Instead of, you can change field rendering.

08/27/08 17:26:45 changed by msaelices

ummm... after dinner, I refreshed myself and I've seen that thing only affect to BoundField. I will think again in a better solution.

08/27/08 19:19:56 changed by msaelices

I will attach a patch. I've found a lot of problems that I've never imaging. I consider that this patch is relatively good thinking in those problems.

Ok, patch have no tests and changes in docs, and possibly there are better variable names. I will wait for core devs review and write tests if core devs accept my patch as "good begin".

08/27/08 19:22:08 changed by msaelices

  • attachment callable_initial_fix_r8640.diff added.

Patch that fixes this extreme bug, but with no tests yet.

08/27/08 19:24:51 changed by msaelices

  • attachment callable_initial_fix_r8640.2.diff added.

Only a typo in a doc string

08/27/08 19:27:32 changed by msaelices

  • has_patch set to 1.
  • needs_tests set to 1.

08/31/08 15:36:07 changed by jacob

  • keywords set to 1.0-blocker.

08/31/08 19:55:40 changed by msaelices

  • attachment callable_initial_fix_with_tests_r8776.diff added.

Patch with tests

08/31/08 19:57:45 changed by msaelices

  • needs_tests deleted.

08/31/08 22:55:24 changed by brosner

  • owner changed from msaelices to brosner.
  • status changed from reopened to new.

08/31/08 22:55:57 changed by brosner

This is looking good. I will give this a through review very soon. Thanks msaelices!

09/01/08 16:29:16 changed by brosner

  • status changed from new to closed.
  • resolution set to fixed.

(In [8816]) Fixed #7975 -- Callable defaults in inline model formsets now work correctly. Based on patch from msaelices. Thanks for your hard work msaelices.


Add/Change #7975 (Admin inlines incorrectly report "this field is required" if inline model has field with default value)




Change Properties
Action