Here's something I find myself doing alot: I've a number of models, typically for blog entries, photos, twitter tweets, links etc, and I want them all displayed in sequence on a 'lifestream' page. I'm hoping others have had the same, and we can come up with a decent solution for this.
There may already be an elegant way of doing this, or a better way will be available in a future version of Django; but there's one way to find out. So here are my thoughts on the matter. I've found a fairly generic way of doing this on the template side via a template tag - see http://www.djangosnippets.org/snippets/890/ - but generating the list of objects to display seems a bit messy. So far, I've been doing this: <code> def order_all_objects(model_list): """ Takes a list of models and sorts items according to pub_date. """ super_query = [] for model in model_list: super_query += [item for item in model.all()] super_query.sort(key=lambda obj: obj.pub_date, reverse=True) return super_query </code> There are at least two problems I have with this approach: The models have to be sent with managers, i.e. the model_list is composed of things like [Entry.live, Link.objects, Photo.live] etc, and all the models must have a pub_date field. Instead, I want to do something like this: <code> class LifeStream(models.Model): """A collection of model instances and their pub_times""" pub_date = models.DateTimeField(default=datetime.datetime.now) content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() class Meta: ordering = ["-pub_date"] def __unicode__(self): return str(self.content_object) def content_created(sender, instance, signal, *args, **kwargs): """ Create a new LifeStream object when content is created """ item = LifeStream(content_object=instance) item.save() dispatcher.connect(content_created, signal=models.signals.post_save, sender=Entry) </code> And then loop through the LifeStream, instead of constructing a new list every time. The pub_date will then be on the LifeStream feed, so it doesn't matter if the original model has date, date_time, published or even no pub_date field at all. As it is, it is hard-coded to monitor only Entry models. I tried to monitor every object, but then of course it also tries to save itself, which triggers a post_save, etc, ad infinitum. Ignoring the post_save signal from LifeStream still leaves other objects I don't want, such as User, Category and any sort of Metadata you could imagine, so that's probably not the way to go. Furthermore, this method will save and show any object, whereas I might only want to show specific ones; only Live entries, for instance. Which brings up another problem: What if I publish an entry marked 'Live', then change my mind and make it a 'Draft'. The entry should then be removed from the Lifestream. I make some changes, and publish it again at a later date. It's pub_date might not change, but in the LifeStream it will be datetime.now. A discrepancy in the order of entries in the LifeStream and the Entry object_list view would result. As I see it, the next step would be some method of listing Models to be included in the life stream - perhaps a setting in the settings file? What this list needs to include is: * The model, required. * The manager, or an arbitrary condition to check against. Don't know what format would be possible here, but this would be optional. * The name of the field that contains the datetime data. Optional. This would be helpful when adding the LifeStream model with some entries already in the database; it would be trivial to loop through everything posted so far and putting it in its right place in the sequence. So, any thoughts? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---

