将属性从方法传递到另一个方法python

时间:2014-05-22 17:52:37

标签: python django oop methods

对不起,我对python中的oop很新,但我想知道如何将tld_objectgather_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()方法

中的数据库

任何帮助都得到了慷慨的赞赏。谢谢。

2 个答案:

答案 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)