1

我正在尝试为我在不同应用程序中定义的所有模型添加基类。我创建了一个包lib/base.py

from django.db import models
import logging

# Get an instance of a logger
logger = logging.getLogger(__name__)

class Base(models.Model):

class Meta:
        abstract = True

现在在不同的主应用程序目录中,我想使用这个基本模型:

from django.db import models
from libs.base import Base
# Create your models here.
class Home(Base):
    msg = models.CharField(max_length=100)

    @classmethod
    def create(cls, msg):
        home = cls(msg=msg)
        # do something with the book
        return home

当我运行服务器时,出现以下错误:

 File "/Users/.../home/models.py", line 2, in <module>
    from libs.base import Base
ImportError: No module named libs.base

我在这里找不到我缺少的东西?

4

1 回答 1

0

您应该将libs模块附加到项目的已安装应用程序中:

settings.py:

...
INSTALLED_APPS = (
    ...
    'home',
    ...
    'libs', # Add this!
)
...
于 2013-08-13T00:53:42.187 回答