Query field across multiple django models

前端 未结 1 1593
夕颜
夕颜 2021-01-03 08:59

I want to create a \"What\'s New\" section that lists all of the database changes in the last day. I\'ve added an \"updated\" field to my models:

class Film         


        
相关标签:
1条回答
  • 2021-01-03 09:33

    I don't know of any way to run a select across multiple tables… So, unless you want to use my suggestion below, you'll just have to loop across all 'updatable' models.

    However, you might want to consider having an UpdatedItems model, which might be something like:

    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 off the top of my head how to figure out which fields are updated… But I'm sure someone's asked about it on Google.

    0 讨论(0)
提交回复
热议问题