2013-12-08 71 views
0

我想要做的和Django一樣。如果我有一個模型投票和模式選擇和定義屬性Poll = ForeignKey(Poll)投票有一個名爲場Poll.choices_set如何獲得創建類「b」的類「a」的名稱?

我有下面的代碼:

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外鍵類別

有人知道如何做到這一點嗎?

+0

請更新的答案......這看起來像一個拗口令 – Goin

+0

我不知道問題是什麼。 – Sebi2020

+0

這是一個叫做元編程*的黑魔法。看到這個:http://python-3-patterns-idioms-test.readthedocs.org/en/latest/Metaprogramming.html Django在內部使用它。我建議不要走這條路,除非你真的**必須。維護這樣的代碼很困難。如果你不確定你是否需要它,那麼你不需要它。 :) – freakish

回答

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'); 
+0

但我想這樣做Django做到這一點,因爲與Django在哪裏是不需要傳遞類C到rel。 – Sebi2020

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 
... 
相關問題