我有一个非django项目,我想使用django模型进行数据访问层。
在requirements.txt
中添加了模型lib
django-model-utils==3.1.1
代码设置如下:
from django.conf import settings
from django.db import models
settings.configure(
DATABASE_ENGINE='django.db.backends.mysql',
DATABASE_NAME='***',
DATABASE_USER='***',
DATABASE_PASSWORD='***',
DATABASE_HOST='***',
DATABASE_PORT='***')
class Bus(models.Model):
class Meta:
db_table = 'my_custom_bus'
bus_name = models.CharField(max_length=20)
bus_description = models.CharField(max_length=100)
但是当我运行上面的代码时,我得到以下错误:
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
为了修复上面的错误,我跑了:
import django
django.setup()
现在,当我尝试,我得到:
Bus doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS
我在这里缺少一些设置,还是在python中只有lib有任何轻量级模型?