I've got items in a list that a user can arbitrarily move around in
whatever order they want.  Additionally, they can add a group of items
to a list in a specific position within that list.

Further, they can sort multiple items at once, meaning they can select
one to many, sequential or non-sequential, and move them as a group to
a new position in the list.

The items will always be moved in order from top to bottom, so if you
select multiple items, whether they're sequential or non, they will be
flattened together into a group sequentially from the top to bottom.

For example:
[1, 2, 3, 4, 5]

If I select 1, 4, and 5 and move them to where id 1 in the list is, it
will look like this:

[1, 4, 5, 2, 3]

I need to refer to items not by their indices, but by their ids.  So,
it's possible that a list might look like this (id-wise)

[27, 31, 45, 7, 21]

When I want to move items, I pass a before_id to determine which item
they should move before. If I pass 0 for before_id, they are moved to
the end of the list.

I've got two possible choices here:

1) Doubly-linked list

Each item in the list has a next_id and prev_id, which are foreign
keys to the Model that those objects are. When I move items in the
list, I calculate and modify the affected rows next/prev id fields.

2) sort_index

A more brute force method is to have each item in the list have a
sort_index, and when I add/move items to the list, I determine where
they're supposed to go and calculate the sort_index for every row in
the table. This actually isn't as easy as it seems because you can't
Array.insert on a QuerySet.  I'm leaning towards a doubly-linked list.


Does anyone have any advice or other, more efficient ways of
accomplishing this?

Thanks!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to