对不起,我对python中的oop很新,但我想知道如何将tld_object
中gather_site()
的值传递给方法gather_path()
class MyClass:
def __init__(self):
print "Class Initialized"
def gather_site(self):
tld_object = Tld.objects.filter(id=3)
return tld_object
def gather_path(self):
path_object = PathsOfDomain.objects.filter(FKtoTld=)
models.py
class Tld(models.Model):
##default table PK here.
##fields here
class PathsOfDomain(models.Model):
##default table PK here.
##fields here
FKtoTld = models.ForeignKey(Tld)
基本上是表Tld
中发生的事情,与PathsOfDomain
有1:M的关系,我希望能够根据来自的tld_object
得到相关路径gather_site()
方法
任何帮助都得到了慷慨的赞赏。谢谢。
答案 0 :(得分:0)
def gather_path(self):
path_object = PathsOfDomain.objects.filter(FKtoTld=3)
我认为应该可以正常工作......
答案 1 :(得分:0)
class MyClass:
def __init__(self):
print "Class Initialized"
def gather_site(self, id):
# Note I'm using get instead of filter when dealing with PKs.
# Only one object is going to be returned.
self.tld_object = Tld.objects.get(id=id)
return self.tld_object
def gather_path(self):
# At this point you have to have self.tld_object already set,
# Either check it exists or give it a default value.
path_object = PathsOfDomain.objects.filter(FKtoTld=self.tld_object.id)