New to django : Having a bug when creating models
hello
i have an unknown error when creating a model class(check the code below ). im following the tutorial on the official django website , but it's not working for some reason, and i have been struggling over it for a while but with no results . is there anything wrong with what im doing ?>>> class a(models.Model):
... pass
...
Trace开发者_开发知识库back (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\django-1.1.2-py2.7.egg\django\db\models\base.py", line 52, in __new__
kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range
>>>
The module containing your models should be named myapp.models
from the Python interpreter's perspective. This error means that it's resolving as just models
.
First, the Python module containing this model needs to be inside of a Django application folder which is on the PYTHONPATH. Try using python manage.py startapp
as explained in the tutorial.
If the model module is definitely in an application folder, make sure that the application folder itself is not part of the PYTHONPATH, only the project folder that contains it.
To help clarify, here's the context of the error in the django source:
# 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]}
I think you should not define your models in command-line interpreter but use application file named models.py
as the previous commenter said.
When django is processing your model it's trying to parse model's application name. In your case model has no any application. So just place your models in myproject/myapplication/models.py
file and then you'll be able to create instances of models in command-line interpreter.
精彩评论