Request field on multiple django models

I want to create a “What's New” section that lists all the database changes for the last day. I added "updated" to my models:

class Film(models.Model): . . . updated = models.DateTimeField(auto_now=True) class Actor(models.Model): . . . updated = models.DateTimeField(auto_now=True) 

Now I want to query all my models to get a list sorted by last modified date. How can I request an “updated” field for multiple models? Is this the most effective way to achieve the main goal?

What if I wanted to be more specific and indicate the actual field that was changed in each model?

+4
source share
1 answer

I don’t know how to run a selection from several tables ... So, if you do not want to use my suggestion below, you just need to iterate over all the "updated" models.

However, you might want to consider the UpdatedItems model, which might be something like this:

 class ItemUpdate(m.Model): when = m.DateTimeField(...) instance = m.GenericForeignKey(...) field = m.CharField(...) old_value = m.CharField(...) new_value = m.CharField(...) 

Then use the post_save signal to populate it:

 form django.db.models.signals import post_save def handle_post_save(sender, instance, *args, **kwargs): ItemUpdate(instance=instance, ...).save() 

I don’t know how to determine which fields are being updated ... But I'm sure someone asked about this on Google.

+3
source

All Articles