You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SOLVED while writing this so, let's keep it for reference…
TL;DR: As mentioned in the documentation:
my_order is the first field in the ordering tuple of the model’s Meta class.
The doc could be more explicit, for example:
my_orderMUST ABSOLUTELY be the first field in the ordering tuple. It's retrieved first from the ModelAdmin, otherwise from the Model.Meta class.
FEATURE REQUEST:(already requested in #155, #198) Please allow to specify the order field in the Model.
It makes sense to sort first on another field in junction models. Especially as that ordering is used elsewhere.
In inlines, the drag&drop works but there's just a global error message Please correct the errors below. but no specific errors in the form/formset… so it seems the error is on a missing or well hidden field.
Tracing down the problem, there's an error on a field in the inline's formset: The inline value did not match the parent instance.
Investigating in the HTML, apparently there's no order value and it's the album_id that is changed with the new sorting order (which explains the error message).
Investigating in the JS, it looks for a default_order_field.
Investigating in the admin.py, there's a _get_default_ordering function that finds it from the first item in the model_admin.ordering or model._meta.ordering… OK 🤬😕🙄
"Conceptual" code…
classMedia(Model): …
classAlbum(Model):
medias=models.ManyToManyField(Media, through="AlbumMedias")
classAlbumMedias(Model):
album, media, orderclassMeta:
ordering= ["album", "order"] # /!\ WRONG! `order` must be the first item.classAlbumAdminInline(SortableAdminInlineMixin, TabularInline):
model=Album.medias.through# Try 1: No fields defined# Try 2: fields = ["media"]# Try 3: fields = ["order", "media"]classAlbumAdmin(SortableAdminMixin, ModelAdmin):
inlines= [AlbumAdminInline]
defsave_form(self, request, form, change):
obj=super().save_form(request, form, change)
breakpoint()
returnobj
Debug:
>/…/my-app/admin.py(131)save_form()
->returnobj
(Pdb) n--Return-->/…/my-app/admin.py(131)save_form()-><Album: Foo>->returnobj
(Pdb) n>/…/django/contrib/admin/options.py(1578)_changeform_view()
->formsets, inline_instances=self._create_formsets(request, new_object, change=notadd)
(Pdb) n>/…/django/contrib/admin/options.py(1579)_changeform_view()
->ifall_valid(formsets) andform_validated:
(Pdb) formsets[0].errors
[{'album': ['The inline value did not match the parent instance.']}, …(sameforallinlinerows, ofcourse)…]
SOLVED while writing this so, let's keep it for reference…
TL;DR: As mentioned in the documentation:
The doc could be more explicit, for example:
FEATURE REQUEST: (already requested in #155, #198)
Please allow to specify the order field in the Model.
It makes sense to sort first on another field in junction models. Especially as that ordering is used elsewhere.
In inlines, the drag&drop works but there's just a global error message
Please correct the errors below.
but no specific errors in the form/formset… so it seems the error is on a missing or well hidden field.Tracing down the problem, there's an error on a field in the inline's formset:
The inline value did not match the parent instance.
Investigating in the HTML, apparently there's no
order
value and it's thealbum_id
that is changed with the new sorting order (which explains the error message).Investigating in the JS, it looks for a
default_order_field
.Investigating in the admin.py, there's a
_get_default_ordering
function that finds it from the first item in themodel_admin.ordering
ormodel._meta.ordering
… OK 🤬😕🙄"Conceptual" code…
Debug:
Admin page before drag&drop:
Admin page after drag&drop:
The text was updated successfully, but these errors were encountered: