theatlantic / django-nested-admin

Django admin classes that allow for nested inlines
http://django-nested-admin.readthedocs.org/
Other
713 stars 99 forks source link

Drag n drop sorting not helpfully documented #93

Open merwok opened 6 years ago

merwok commented 6 years ago

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.

Thanks in advance.

fdintino commented 5 years ago

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.

brandenhall commented 5 years ago

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!

fdintino commented 5 years ago

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)
brandenhall commented 5 years ago

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.

fdintino commented 5 years ago

I would appreciate that. Thanks!