Get all related Django model objects

后端 未结 9 1348
攒了一身酷
攒了一身酷 2020-11-27 09:46

How can I get a list of all the model objects that have a ForeignKey pointing to an object? (Something like the delete confirmation page in the Django admin before DELETE C

9条回答
  •  执笔经年
    2020-11-27 10:42

    The following is what django uses to get all related objects

    from django.db.models.deletion import Collector
    collector = Collector(using="default")
    collector.collect([a])
    
    print collector.data
    

提交回复
热议问题