Skip to main content
 首页 » 编程设计

python之Django 模型类在管理器中为 None

2025年05月04日19haluo1

我在运行 Django 应用程序的服务器上遇到了一个非常奇怪的错误。该错误无法在我的本地开发服务器上重现。

我有这个模型和它的经理:

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 的东西,因为我无法在我的本地机器上重现它,但我不知道去哪里看......

谢谢!

请您参考如下方法:

使用self.model从管理器访问模型:

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