[英]How to iterate over all child in Django?
If many models in Django
have foreign key relationship to a common model then how will it be possible to iterate over all the child models one by one. 如果
Django
许多模型与公共模型具有外键关系,那么如何遍历所有子模型。
I know about parent.child_set.all()
, but I want to know if there is something that can be helpful to access child using a variable. 我知道
parent.child_set.all()
,但是我想知道是否存在一些有助于使用变量访问child的东西。
hard to tell what you're trying to do....but a simple getattr might help 很难说出您要做什么....但是一个简单的getattr可能会有所帮助
child_var = 'child1'
qs = getattr(ParentModelInstance, child_var+'_set').all()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.