Reputation: 6841
I am learning Django going through the online book, and am presently stuck at Chapter 6. In the Adding Your Models to the Admin Site section the reader is required to create a file named admin.py
, within the books app, with this content:
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)
This is supposed to make these models available for editing at the admin site, but what I get is the following:
ImportError at /admin/
No module named books.models
In the Python command line I get a similar error:
>>> from mysite.books.models import Book
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysite.books.models
These are the contents of the models.py
file:
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 Meta:
ordering = ['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
I have strictly followed the instructions in the book to this point, thus I should have an exact copy of the code used. The file structure was created automatically by Django:
books
__init__.py
admin.py
models.py
tests.py
views.py
mysite
templates
__init__.py
settings.py
urls.py
views.py
wsgi.py
manage.py
What would be the correct way of importing the models
file in the admin
module?
Upvotes: 1
Views: 682
Reputation: 45575
This is pretty old book and some things are deprecated in current version of Django.
Try this import:
from books.models import Publisher, Author, Book
instead of:
from mysite.books.models import Publisher, Author, Book
Upvotes: 3