Selected Item Order on Change Issue

26 views
Skip to first unread message

Chad Files

unread,
Aug 19, 2014, 7:54:43 AM8/19/14
to django-s...@googlegroups.com
I have a scenario where I need to preserve the order of the selected items when a model object is changed. Is this possible? Here is the setup...

Say I have a model object that has a many-to-many relationship with another model. This relationship is displayed in the UI via the AutoComboboxSelectMultipleWidget. Basically a standard setup for django-selectables. The problem I have is when a user modifies the many-to-many list I need to keep the order, but it changes. 

For example, assume the list of items is: [A, B, C, D].
The user selected [D, A, C] when the object was created, the order is preserved.
Then the user edits the form and adds [B], this should make the list [D, A, C, B].
However, what I get is [A, B, C, D].

Is there a way to change this behavior? Thanks!

Mark Lavin

unread,
Aug 19, 2014, 11:37:37 AM8/19/14
to django-s...@googlegroups.com
django-selectable currently adds newly added items in after the text input rather than after the last hidden input which should put the new items first in POST which is partially breaking the desired order. Changing this would be a first step.

However, if your many-to-many doesn't have a through model which defines the order then your ordering is likely depending on the PK of the auto-generated through model table which won't work for DB backends which reuse PKs (MySQL and SQLite for example). If you do have a defined ordering then updating the form to pass them in the correct order (should probably be done automatically by the M2M field) when initialized and set the order on save assuming the previous issue is fixed so that the order in the POST is the same as the order as they were added.


--
You received this message because you are subscribed to the Google Groups "django-selectable" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-selecta...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chad Files

unread,
Aug 22, 2014, 4:09:16 AM8/22/14
to django-s...@googlegroups.com
Thank you for the reply.

On Tuesday, 19 August 2014 10:37:37 UTC-5, Mark Lavin wrote:
django-selectable currently adds newly added items in after the text input rather than after the last hidden input which should put the new items first in POST which is partially breaking the desired order. Changing this would be a first step.

How do I go about this? Is there an option where I can set this or am I going to have to do some sort of custom form save?
 
However, if your many-to-many doesn't have a through model which defines the order then your ordering is likely depending on the PK of the auto-generated through model table which won't work for DB backends which reuse PKs (MySQL and SQLite for example). If you do have a defined ordering then updating the form to pass them in the correct order (should probably be done automatically by the M2M field) when initialized and set the order on save assuming the previous issue is fixed so that the order in the POST is the same as the order as they were added.

It is depending on the PK of the auto generated through and that is what I want (I think). I just did a quick check and somewhere in the stack, the items in the through model get deleted and added back when I do an update. Is django or the selectables handler doing that? How can I change this behavior?

Chad Files

unread,
Aug 22, 2014, 4:47:32 AM8/22/14
to django-s...@googlegroups.com
I did some research on the intermediate model and I get why I need an order. The thing I do not understand now is how to make that work with the selectables. Is there an example of this somewhere?
Reply all
Reply to author
Forward
0 new messages