views:

188

answers:

1

I have multiple abstract models similar to this

Class Article(models.Model):
    title = models.CharField()
    body = models.TextField()
    created_date = models.DateTimeField()
    author_name = models.CharField()

class Video(models.Model):
    title = models.CharField()
    body = models.TextField()
    created_date = models.DateTimeField()
    video_asset = models.CharField()

Now on a specific page I would like to aggregate these two models into a list based on their created_date. All the models I want I know will have a title and a created_date so I can just do something like this:

<ul>
{% for object in object_list %}
    <li>{{ object.title }} on {{ object.created_at }}</li>
{% endfor %}
</ul>

I can count on those fields to be there with no issue.

I've thought about creating an additional model and using generic foreign keys. Kind of like an aggregate model. then every time a new object was created I would just signal one of those to be created and then just pull from this generic table. I dont really like this idea though. Seems highly redundant.

Any thoughs?

update: I found this entry http://stackoverflow.com/questions/313137/ but it wont work in my case as I am using abstract model class for these (I just didnt show it in the example to keep it simple and clear as possible). I looked into inheritance instead of abstraction but i thought the performance hit on constant joins would take a toll. maybe not

A: 

Just order both models by created_date (or created_at, you used both) in your view and merge the results into a single list. This list can then be used in your template. No model inheritance stuff or aggregated models needed.

stefanw