2012-02-20 46 views
2

搜索谷歌提供的文檔並瀏覽所以我還沒有找到一種方法來檢索在db.Property對象上設置的選項(我想檢索它以創建基於模型)。檢索db.Property選項

我使用下面的食譜來做我需要的東西,這是正確的嗎?還有其他的方式嗎? (更簡單,更優雅,更Python等)

對於這樣一個模型:

class PhoneNumber(db.Model): 
    contact = db.ReferenceProperty(Contact, 
           collection_name='phone_numbers') 
    phone_type = db.StringProperty(choices=('home', 'work')) 
    number = db.PhoneNumberProperty() 

我做了以下修改:

class PhoneNumber(db.Model): 
    _phone_types = ('home', 'work') 
    contact = db.ReferenceProperty(Contact, 
           collection_name='phone_numbers') 
    phone_type = db.StringProperty(choices=_phone_types) 
    number = db.PhoneNumberProperty() 

    @classmethod 
    def get_phone_types(self): 
     return self._phone_types 

回答

1

您應該能夠使用PhoneNumber.phone_type.choices。如果你想要,你也可以把它變成一個類的方法:

@classmethod 
def get_phone_types(class_): 
    return class_.phone_type.choices 

你可以決定你是否喜歡類方法的方法。

不要忘了Python的dir內置!探索物體時非常有用。

+0

我完全錯過了使用目錄。謝謝。 – 2012-02-21 01:18:42

+0

在ndb中,該屬性似乎是'_choices'。 – 2014-08-13 01:17:49