Ordered list of objects?

17 views
Skip to first unread message

Nathan R. Yergler

unread,
Apr 3, 2007, 10:09:53 PM4/3/07
to django...@googlegroups.com
I'm working on an app for a client, and one of the requirements is
that they be able to re-order objects in the admin interface. I know
exactly how I'd do with with an object database, but the ideas I've
come up with for doing it with Django all involve using some tracking
field, and then updating that to contain the relative order of
objects.

So... am I missing something obvious here that will let me do with
with a minimal amount of pain?

Thanks,

Nathan

Atilla

unread,
Apr 4, 2007, 4:47:00 AM4/4/07
to django...@googlegroups.com

What kind of ordering does it have to be and on what amount of items?
Perhaps it could be useful to create a separate Model that defines the
order of items, with a relation to the actual model that defines the
objects themselves. The biggest issue is how to create usable controls
that define the ordering, which is not always a simple thing to
implement.

Nathan R. Yergler

unread,
Apr 4, 2007, 9:57:34 AM4/4/07
to django...@googlegroups.com

Arbitrary ordering of dozensis it of items, so I'm not sure an
external model would help.


>
> >
>

EBML...@gmail.com

unread,
Apr 20, 2007, 3:59:37 PM4/20/07
to Django users
I don't know if this is the "right" solution or not. In my home grown
framework I've got a doubly linked list implementation.

My intention in Django is to write an application Ordered_List that
would be a functioning doubly linked list with minimal data fields
(currently just a name for each list to keep me sane during testing).
I'm thinking that I could import from this app and sub class from it
without having it in INSTALLED_APPS. To be able to mix-in arbitrary
ordering with any other model.

Brand new to Django, but if it goes well I'll post the code.

If someone has a better/easier solution feel free to share it.

--Lucki

On Apr 4, 9:57 am, "Nathan R. Yergler" <nat...@yergler.net> wrote:

EBML...@gmail.com

unread,
May 1, 2007, 4:39:47 PM5/1/07
to Django users
I've got the solution, you can download it here:
http://purplesquirrelgroup.com/order_list.tar.gz

Here's the readme:

To use these models you will want to do the following:

For your Collection class:

from order_list.models import Order_List
from django.db import models

class Collection( Order_List, models.Model):
name = models.CharField('name', maxlength=80) #Feel free to
customize Maxlength

# I think there should be a better way to do this, but if you want to
use the
# can_reorder permision from the base class you'll need to do this:

class Meta:
permissions = Order_List.Meta.permissions


And for your Item class:

from order_list.models import Order_List_Item
from django.db import models

class Item(Order_List_Item, models.Model):
# This /must/ be called list, and relate_name /must/ be
'_items'
list = models.ForeignKey(Collection, verbose_name='collection
item', related_name='_items')
_next = models.ForeignKey('self', verbose_name=_("next"),
null=True, blank=True, related_name='_prev')

# I like to do this:
def _get_collection(self):
return self.list
collection = property(_get_collection)


By applying this skelleton to any model you can make it arbitrarily
ordered. When you create new objects other then the first in
Collection be sure you use insert_before or insert_after with an
existing item rather then just save to preserve list sanity.

Reply all
Reply to author
Forward
0 new messages