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.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---