Skip to main content
 首页 » 编程设计

python之代码完成 Pydev Django 模型外键字段

2025年05月04日25zhengyun_ustc

Eclipse/pydev 中的代码完成对我来说效果不错。但是,有一件事不能像我想要的那样工作。考虑以下 Django 模型:

class ReletionTree(models.Model): 
    mother = models.ForeignKey('RelationTree', blank=True, null=True) 
    father = models.ForeignKey('RelationTree', blank=True, null=True) 
    name = models.CharField() 
 
rt = RelationTree.objects.get(name='Mary') #assume unique on Mary 

现在的问题:

rt.        #--> will code complete and give me options mother/father/name 
rt.mother. #--> will not code complete into mother/father/name, it will code  
           #    complete as if isinstance(rt.mother, models.ForeignKey) (I think) 

有没有办法让 Pydev 明白我想让它编写完整的外键 就好像它们是它指向的类型的位置(在上面的例子中是 RelationTree 而不是 models.ForeignKey)

谢谢,大卫

请您参考如下方法:

我很怀疑(我也做Django和Eclipse),因为Pydev不够聪明,无法理解Django用来将father = models.ForeignKey()转换成father = RelationTree() 或其他。

Python 对自动完成器来说真的很难解析,而 PyDev 和 PyLint 在元类方面似乎完全放弃了。 (pylint 总是提示我的模型类没有 objects 成员!)