我可以使用模型 class 因为其他文件是手动创建的吗?
Can I Use models class since other file create manually?
文件是
app
__init__.py
admin.py
import.py
models.py
tests.py
views.py
我手动创建了一个文件调用 import.py,其中包含:
from models import Category
在 models.py 中包含
from django.db import models
class Category(models.Model):
name = models.CharField(max_length = 25, unique = True)
description = models.TextField(blank=True)
def __unicode__(self):
return "%s, %s" % (self.name, self.description)
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
然后我需要执行 import.py 文件,因为 shell,但我需要使用 Django 的模型。
我执行
python manage.py shell < .\models\import.py
我有这个错误:
>>> Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: cannot import name Category
您应该指定完整的模块名称:
from app.models import Category
或者使用相对导入:
from .models import Category # Note the dot before the `models`
文件是
app
__init__.py
admin.py
import.py
models.py
tests.py
views.py
我手动创建了一个文件调用 import.py,其中包含:
from models import Category
在 models.py 中包含
from django.db import models
class Category(models.Model):
name = models.CharField(max_length = 25, unique = True)
description = models.TextField(blank=True)
def __unicode__(self):
return "%s, %s" % (self.name, self.description)
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
然后我需要执行 import.py 文件,因为 shell,但我需要使用 Django 的模型。 我执行
python manage.py shell < .\models\import.py
我有这个错误:
>>> Traceback (most recent call last):
File "<console>", line 1, in <module>
ImportError: cannot import name Category
您应该指定完整的模块名称:
from app.models import Category
或者使用相对导入:
from .models import Category # Note the dot before the `models`