1

我做了一个名为books的项目mysite,并使用startapp创建了一个名为books的项目。我创建了管理站点,它运行成功。但是当我尝试将模型添加到我的管理站点时,提到这些事情时发生了错误。

ImportError at /admin/
No module named books.models

实际上我是admin.py在books文件夹中创建的,然后我编写了以下代码。

from django.contrib import admin
from mysite.books.models import Publisher, Author, Book

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)

模型.py:

from django.db import models

class Publisher(models.Model):
    name = models.CharField(max_length=30)
    address = models.CharField(max_length=50)
    city = models.CharField(max_length=60)
    state_province = models.CharField(max_length=30)
    country = models.CharField(max_length=50)
    website = models.URLField()

    def __unicode__(self):
        return self.name

class Author(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=40)
    email = models.EmailField()

    def __unicode__(self):
        return u'%s %s' % (self.first_name, self.last_name)

class Book(models.Model):
    title = models.CharField(max_length=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

    def __unicode__(self):
        return self.title

那么,如何解决这个问题呢?

4

2 回答 2

0

试试这个。。

from django.contrib import admin
from models import Publisher, Author, Book

admin.site.register(Publisher)
admin.site.register(Author)
admin.site.register(Book)
于 2013-06-12T10:41:32.987 回答
0

我会尝试

from books.models import Publisher, Author, Book
...

如果书籍是您的应用程序的名称

于 2013-06-12T10:43:44.863 回答