Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果其中的许多模型Django与一个公共模型具有外键关系,那么如何一个一个地迭代所有子模型。
Django
我知道parent.child_set.all(),但我想知道是否有一些东西可以帮助使用变量访问 child。
parent.child_set.all()
很难说出你想要做什么......但一个简单的 getattr 可能会有所帮助
child_var = 'child1' qs = getattr(ParentModelInstance, child_var+'_set').all()