码迷,mamicode.com
首页 > 编程语言 > 详细

Python Django shell 调试

时间:2014-12-21 16:44:34      阅读:499      评论:0      收藏:0      [点我收藏+]

标签:

Python Django 调试

>>> class Person(models.Model):
...     first_name = models.CharField(max_length = 50)
...     last_name = models.CharField(max_length = 50)
...     def __unicode__(self):
...         return u‘%s %s‘ %(self.first_name ,self.last_name)
... 
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/django/db/models/base.py", line 117, in __new__
    kwargs = {"app_label": package_components[app_label_index]}
IndexError: list index out of range

模型的定义必须在应用程序中-你看到那里它试图采取 name model_module 错误-这应该有一些像为 project\appname\models.py-project.appname.models,并获得该应用程序的名称,appname 来。在交互式的控制台中,该模块 name 是 ‘main‘,所以它会失败。

为了解决这个问题,你需要在元类 自己指定的 app_label,

>>> from django.db import models
>>> class Poll(models.Model):
...     question = models.CharField(max_length=200)
...     pub_date = models.DateTimeField(‘date published‘)
...     class Meta:
...         app_label = ‘test‘

解释为什么你可以做到,看看在回溯(traceback)的时候,/usr/lib/python2.7/site-packages/django/db/models/base.py 中提到该文件:

if getattr(meta, ‘app_label‘, None) is None:
    # Figure out the app_label by looking one level up.
    # For ‘django.contrib.sites.models‘, this would be ‘sites‘.
    model_module = sys.modules[new_class.__module__]
    kwargs = {"app_label": model_module.__name__.split(‘.‘)[-2]}
else:
    kwargs = {}

元类在哪里定义呢?,见上方在该文件中。

Python Django shell 调试

标签:

原文地址:http://my.oschina.net/innovation/blog/358956

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!