2015-02-08 45 views
2

我在運行django應用程序的服務器上收到了一個非常奇怪的錯誤。該錯誤在我的本地開發服務器上無法重現。Django模型類在管理器中爲None

我有這樣的模型,它的經理:

class CardManager(models.Manager): 

    def get_by_identifier(self, card_identifier): 
     ... 
     for possible_suit in Card.SUITS: 
      ... 

class Card(models.Model): 
    objects = CardManager() 
    SUITS = ((1, 'Clubs'), ...) 

這是錯誤:

AttributeError at /game/playcard/2/S1/ 

'NoneType' object has no attribute 'SUITS' 

和回溯:

File "local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    115. response = callback(request, *callback_args, **callback_kwargs) 
File "local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view 
    25. return view_func(request, *args, **kwargs) 
File "views.py" in play_card 
    155. card = Card.objects.get_by_identifier(card_identifier) 
File "models.py" in get_by_identifier 
    16. for possible_suit in Card.SUITS: 

運行Django 1.5(是的,我知道),Python 2.7和uwsgi

有何想法?因爲我無法在本地機器上重現它,所以我傾向於某種特定的uwsgi,但我不知道該在哪裏尋找...

謝謝!

回答

1

使用self.model從管理程序訪問模式:

for possible_suit in self.model.SUITS: 
    ... 
+0

真棒,謝謝!是否有一個原因?它在文檔中提到過嗎?我以前找不到任何東西...... – Gargamel 2015-02-08 09:15:51