#34129: Admin list_editable failed to edit
-------------------------------+--------------------------------------
     Reporter:  Djing          |                    Owner:  nobody
         Type:  Bug            |                   Status:  new
    Component:  contrib.admin  |                  Version:  3.2
     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 Djing):

 * cc: Djing (added)


Comment:

 The exception is DoesNotExist, and it is thrown by the **to_python**
 method of django.forms.models.**ModelChoiceField**.

 {{{
     def to_python(self, value):
         if value in self.empty_values:
             return None
         try:
             key = self.to_field_name or 'pk'
             if isinstance(value, self.queryset.model):
                 value = getattr(value, key)
             value = self.queryset.get(**{key: value})
         except (ValueError, TypeError, self.queryset.model.DoesNotExist):
             raise ValidationError(self.error_messages['invalid_choice'],
 code='invalid_choice')
         return value
 }}}

 The **ModelChoiceField** and it's queryset come from the **add_fields**
 method of django.forms.models.**BaseModelFormSet**. This method builds
 field for pk according to the comment.

 {{{
     # Omit the preceding part...
     if isinstance(pk, (ForeignKey, OneToOneField)):
         qs = pk.remote_field.model._default_manager.get_queryset()
     else:
         qs = self.model._default_manager.get_queryset()
     qs = qs.using(form.instance._state.db)
     if form._meta.widgets:
         widget = form._meta.widgets.get(self._pk_field.name, HiddenInput)
     else:
         widget = HiddenInput
     form.fields[self._pk_field.name] = ModelChoiceField(qs,
 initial=pk_value, required=False, widget=widget)
     # Omit the latter part...
 }}}

 The qs here use the default manager of Model which only shows valid items,
 so invalid item can't be found.

 And I noticed that **BaseModelFormSet** has the correct queryset which
 shows all items when it is created by **ModelAdmin**.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34129#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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018431922ade-4b7f3f57-f87c-4f63-a364-670052280df9-000000%40eu-central-1.amazonses.com.

Reply via email to