我在models.py文件中定义了两个类:
class Branch_Circle(models.Model):
...
trunk_circle = models.ForeignKey('Trunk_Circle', null=True)
class Trunk_Circle(models.Model):
...
def create_branch_circle(self):
branch_circle = Branch_Circle(trunk_circle=self)
branch_circle.save()
return branch_circle
使用shell我首先实例化一个Trunk_Circle对象,然后调用它的'create_branch_circle'方法并期望它创建一个Branch_Circle对象。它没有:
import Trunk_Circle
import Branch_Circle
r = Trunk_Circle
s = r.create_branch_circle
当我致电Branch_Circle.objects.all()
时,它是空的。此外,'s'的类型是<bound method Trunk_Circle.create_branch_circle of <Trunk_Circle: Trunk_Circle object>>
答案 0 :(得分:2)
要实例化对象或调用方法,您必须使用括号 ()
:
r = Trunk_Circle()
s = r.create_branch_circle()