django-nested-admin icon indicating copy to clipboard operation
django-nested-admin copied to clipboard

Drag n drop sorting not helpfully documented

Open merwok opened this issue 6 years ago • 5 comments

Hello! I tried to follow the grappelli documentation to understand how to enable drag-and-drop ordering with nested-admin but don’t understand the steps needed.

  • Does it only work with tabular inlines?
  • Where is the drag widget supposed to be?
  • Is there an example somewhere?

Thanks in advance.

merwok avatar Mar 07 '18 19:03 merwok

Adding sortable_field_name = 'your_order_field' should be all that's necessary to enable sorting, and it should work for both stacked and tabular inlines. For stacked inlines, the inline header is the draggable object, whereas for tabular inlines there should be a drag icon. I believe (though am not certain) that the position field on the model needs to have default=0. You can find a few examples in the code for the unit tests.

fdintino avatar Feb 01 '19 16:02 fdintino

Just to add on here, is there a known way to hide the position field when doing a sortable inline? Grappelli has GrappelliSortableHiddenMixin - is there something equivalent here that I'm missing? Thanks!

brandenhall avatar Feb 12 '19 15:02 brandenhall

I could add that mixin, that seems useful. In our own projects that use django-nested-admin we've made position a PositiveIntegerField and then added this to our admin classes:

formfield_overrides = {
    models.PositiveSmallIntegerField: {"widget": forms.HiddenInput()}
}

or

class MyAdmin(nested_admin.StackedInlineModelAdmin):
    def formfield_for_dbfield(self, field, **kwargs):
        if field.name == 'position':
            kwargs.setdefault('widget', HiddenInput)
        return super(MyAdmin, self).formfield_for_dbfield(field, **kwargs)

fdintino avatar Feb 12 '19 16:02 fdintino

Thanks for the quick response and for this library. I've found it really useful in a bunch of projects over the past year. If you'd like I could take a stab at the mixin and send you a PR.

brandenhall avatar Feb 12 '19 16:02 brandenhall

I would appreciate that. Thanks!

fdintino avatar Feb 12 '19 16:02 fdintino