Python Django Debugging
>>> class person (models. Model): ... First_Name = models. Charfield (max_length = ...) Last_Name = models. Charfield (max_length = ...) def __unicode__ (self): ... return u '%s '% (Self.first_name, self.last_name) ... Traceback (most recent): 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
The definition of the model must be in the application-you see there it tries to take the name model_module Error-This should have some like for project\appname\ Models.py-project.appname.models, and get the name of the application, appname come. The module name is 'main' in the interactive console, so it will fail.
In order to solve this problem, you need to specify the App_label in the Meta class,
>>> 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 '
Explain why you can do it, see in Backtracking (Traceback),/usr/lib/python2.7/site-packages/django/db/models/base.py mentions the file:
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 is ' sites '. Model_module = sys.modules[new_class.__module__] Kwargs = {"App_label": Model_module.__name__.split ('. ') [-2]} else: Kwargs = {}
Where is the meta-class defined? , see above in the file.
Python Django Shell Debugging