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

Feature request: independent order counters for each specific foreign key value

Open architux opened this issue 8 years ago • 3 comments

Hello and thanks for your useful work!

I've plugged your Django battery for my custom model, say, Model_1 that has a foreign key to other model, say, Model_2.

Order field automatically increments on creating new values in Model_1, but it's the same incrementation throughout the whole model.

pk title order model_2_fk
1 Title 1 1 fk_one
2 Title 2 2 fk_one
3 Title 3 3 fk_two
4 Title 4 4 fk_two
5 Title 5 5 fk_three
6 Title 6 6 fk_three

It would be great to start ordering from 1 for each foreign key value linked with the model, to make separate ordering for each so-called 'category' coming from the FK value.

pk title order model_2_fk
1 Title 1 1 fk_one
2 Title 2 2 fk_one
3 Title 3 1 fk_two
4 Title 4 2 fk_two
5 Title 5 1 fk_three
6 Title 6 2 fk_three

So, basically, when admin adds a new value in Model_1 the plugin should get the max value of ordering field for the current FK value from another model and increment ordering field for each FK values independently.

There should be such options for admin.py:

  • [ ] a BooleanField to turn this behaviour on or not, probably being False by default.
  • [ ] an option to specify which FK field of the current model should be taken into account in such context.

I hope I've explained my request clear enough. I'd be glad for some implementation in the future. ;-)

architux avatar May 29 '17 11:05 architux

Also having this on any other field (not just FK, but maybe a CharField with choices) would be nice. I guess no one is working on this, I might as well add this functionality.

mcbarin avatar Sep 21 '21 09:09 mcbarin

I somehow have difficulties to understand how this shall work. If items are filtered by title and then sorted using the provided UI, updating the order field would be easy. But what, if we show all items from Model_1? Say, you moved item with pk=5 into the seconds position, what would order and title be afterwards?

jrief avatar Sep 27 '21 13:09 jrief

My first suggestion would be removing the requirement of having order field the first item on model's ordering field. Rather than having a boolean and enabling/disabling this functionality, we might say order could be the second item like this in model class: ordering = ('model_2_fk', 'order', )

Here, we can have another requirement that says order field can be first or second item in Model.ordering. Since we know the type of the field using Model class, we can determine which field is the order field (integerfield, positiveintegerfield etc.), and which one is the prior order field that we discuss.

When it comes to your question @jrief ; I think the scenario of moving pk=5 to the second place should be illegal in this case and should raise an error since it's against the natural ordering that we use.

I'd like to hear your opinion on this though. Does it make sense to you @jrief ?

mcbarin avatar Sep 29 '21 15:09 mcbarin