如何获取创建类'b'的类'a'的名称?

时间:2013-12-08 12:28:52

标签: python django class object

我想和Django一样。如果我有模型轮询和模型选择并定义属性Poll = ForeignKey(Poll),则轮询会有一个名为Poll.choices_set的字段

我必须遵循以下代码:

class rel(object):
    def __init__(self,cls):
        # here I want to get the name of class c there this class is instanced.
        setattr(cls,[name_of_class_c] +"_set",cls())

class a(object):
    pass

class c(object):
    somevar = 3
    relationField = rel(a);

abc = a();
print abc.c_set.somevar

此处class a类投票class c选择类class rel外键类

有人知道如何实现这个目标吗?

2 个答案:

答案 0 :(得分:0)

如果您只想设置属性,则可以将类本身作为参数传递:

class rel(object):
    def __init__(self,cls, parent):
        setattr(cls,parent.__name__ +"_set",cls())
class c(object):
    somevar = 3
    relationField = rel(a, c);

或者,如果您只想要名称:

class rel(object):
    def __init__(self,cls, name):
        setattr(cls,name+"_set",cls())
class c(object):
    somevar = 3
    relationField = rel(a, 'c');

答案 1 :(得分:0)

找到了一个肮脏的解决方法,但还不完美:

class Model:
    pass
class rel(object):
    def __init__(self,cls):
    self.relation = cls;

class a(Model):
    pass

class c(Model):
    some_other = 4
    a = rel(a)

class b(Model):
    somevar = 3
    a = rel(a);

### Some sort of hack ###
gl = list(globals())
for v in gl:
    if type(eval(v)).__name__ == 'classobj':
        cls = eval(v);
        if len(cls.__bases__) > 0 and cls.__bases__[0].__name__ == "Model":
            for atr in dir(cls):
                if type(getattr(cls,atr)).__name__ == "rel":
                    relL = getattr(cls,atr).relation;
                    print relL.__name__, "->", cls.__name__
                    setattr(relL,cls.__name__ + "_set",cls)
### End - Some sort of hack ###

d = a();
print a.b_set.somevar
print a.c_set.some_other

输出:

a -> c
a -> b
3
4

编辑:一些更好的肮脏黑客:

class Model:
    ### Some sort of hack ###
    def __init__(self):
        gl = list(globals())
        print self.__class__.__name__, "Relations:"
            for v in gl:
                if type(eval(v)).__name__ == 'classobj':
                    cls = eval(v)
                    if len(cls.__bases__) > 0 and cls.__bases__[0].__name__ == "Model":
                        for atr in dir(cls):
                            if type(getattr(cls,atr)).__name__ == "rel":
                                rell = getattr(cls,atr).relation;
                                if isinstance(self,rell):
                                    print rell.__name__, "<-", cls.__name__
                                    setattr(rell,cls.__name__ + "_set",cls)
    ### End - Some sort of hack ###

class rel(object):
    def __init__(self,cls):
    self.relation = cls;

class a(Model):
    pass

class c(Model):
    some_other = 1
    a = rel(a);

class d(Model):
    string = "Hello"
    c = rel(c)
class b(Model):
    somevar = 3
    a = rel(a);
    d = rel(d);


a = a();
c = c();
print "\nVars:"
print "somevar",":",a.b_set.somevar
print "some_other",":",a.c_set.some_other

输出:

a Relations:
a <- c
a <- b
c Relations:
c <- d
...