allstar
allstar

Reputation: 1205

django admin.py for an app

I'm following the tutorial contained here:

http://www.djangobook.com/en/2.0/chapter06.html

The instructions are to create a file called admin.py in the app folder with this code:

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)

However, after I do this I get an error at the second line that says:

No module named books.models

I found a similar question here but didn't understand the answer:

Django admin page error

Thank you for your help!

Upvotes: 0

Views: 857

Answers (3)

allstar
allstar

Reputation: 1205

I believe I found the answer here:

ImportError: No module named books.models

For whatever reason, because in the settings file I just have 'books' under installed apps, my admin.py should read:

from books.models import Publisher, Author, Book

instead of

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

Now I'm not sure what the rule here is, but this fixed my issue...

Upvotes: 0

zeantsoi
zeantsoi

Reputation: 26193

Have you actually created your Book model yet? The following is the model definition from the tutorial you're following:

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

Upvotes: 1

22222222
22222222

Reputation: 581

Make sure you have a __init__.py in the books directory so Python recognizes it as a module.

Upvotes: 1

Related Questions