Manually ordering content

5 views
Skip to first unread message

Bret W

unread,
Feb 16, 2008, 1:15:14 PM2/16/08
to Django users
Hello all-

I'm trying to figure out the best way to manually order objects within
a template.

I have a couple of pages on which I would like to be able to hand-pick
content to fill certain areas.

I searched through the treads of this list, and couldn't find a "best
practice," so I thought I'd throw the question out again.

Here's what I've come up with:

== Model ==
page/models.py

from django.db import models
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class Page(models.Model):
name = models.CharField(max_length=200)
content_type = models.ForeignKey(ContentType)

class Meta:
unique_together = (('name', 'content_type'))

class Admin:
pass

def __unicode__(self):
return self.name

class OrderedItem(models.Model):
position = models.PositiveIntegerField()
page = models.ForeignKey(Page)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type',
'object_id')

class Meta:
unique_together = (('position', 'page', 'content_type'))

== End Model ===

I can then add order = generic.GenericRelation(OrderedItem) to any
other model.

This will allow me to do:
>>> p = Page(name='homepage')
>>> p.save()

(Entry is an example model, one that has order =
generic.GenericRelation(OrderedItem))
>>> e = Entry(headline='test')
>>> e.save()

>>> o1 = OrderedItem(position=1,page=p,content_object=e)
>>> o1.save()

>>> e2 = Entry(headline='fubar')
>>> e2.save()

>>> o2 = OrderedItem(position=2,page=p,content_object=e2)
>>> o2.save()

>>> ordered_items OrderedItem.objects.order_by('position').filter(page=p)
>>> for item in ordered_items:
... print item.content_object.headline
...
test
fubar

So, I could pass ordered_items to a template, which would allow me to
display objects in an order I specified, which is great.

So, I have two questions:

1. Is there a better way to do this?
2. I can't quite wrap my head around how to administer these items
via the admin interface. Is there a way to do so without creating
custom admin templates?

Thanks for any guidance.



Bret W

unread,
Feb 18, 2008, 4:59:25 PM2/18/08
to Django users
I never really heard anything back on this, so I built something based
on the models in my last message.

The details and code are here:
http://www.nyquistrate.com/django/orderedlist/

I'd appreciate any feedback you might have. Hopefully others will
find this code useful.

Thanks,
Bret

Fco. Javier Nievas

unread,
Feb 18, 2008, 5:07:53 PM2/18/08
to django...@googlegroups.com
Maybe you could use the "ordering" attribute at META class, to avoid using order_by in every request.

Wish it helps

2008/2/18, Bret W <bretw...@gmail.com>:

Bret W

unread,
Feb 18, 2008, 5:16:18 PM2/18/08
to Django users
While that will allow content items to be ordered by a field in the
model, I'm looking for a way to arbitrarily order items. I don't
think the ordering attribute will solve this problem.
Reply all
Reply to author
Forward
0 new messages