0

As a complete beginner, I really hope I'm missing something obvious here, and that someone with experience can easily point out my mistake.

I'm in the first steps of creating some Django models, and can't figure out how to resolve an error I get when I try to make migrations. From my research, it looks like this error is vague. I have no idea what it means by saying there's no attribute 'model'.

Here's the traceback:

Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
utility.execute()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 332, in execute
self.check()
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 364, in check
include_deployment_checks=include_deployment_checks,
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\management\base.py", line 351, in _run_checks
return checks.run_checks(**kwargs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\core\checks\registry.py", line 73, in run_checks
new_errors = check(app_configs=app_configs)
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\contrib\admin\checks.py", line 22, in check_admin_app
errors.extend(site.check(app_configs))
File "C:\Program Files (x86)\Python36-32\lib\site-packages\django\contrib\admin\sites.py", line 79, in check
if modeladmin.model._meta.app_config in app_configs:
AttributeError: 'Book' object has no attribute 'model'

And here's the model code:

class Author(models.Model):
    name = models.CharField(max_length=80, blank=False, null=False, unique=True)

class Book(models.Model):
    title = models.CharField(max_length=150)
    description = models.TextField(blank=True, null=True)
    series = models.CharField(max_length=150, blank=True, null=True)
    authors = models.ManyToManyField(Author, blank=True)
    finished = models.BooleanField(default=False, verbose_name="Finished")
    # image = models.ImageField()

    def list_authors(self):
        return ", ".join([author.name for author in self.authors.all()])

class Narrator(models.Model):
    narrator = models.CharField(max_length=80)

class Audiobook(Book):
    length = models.TimeField(blank=False, null=False)
    narrator = models.ForeignKey(Narrator, on_delete=models.SET_NULL, blank=False, null=True)

I've gone through a few tutorials, and read whatever Django documentation I thought might be relevant, but I'm still missing something. Any ideas, or general pointers would be greatly appreciated.

EDIT: Adding my admin.py. I commented this all out and the migration worked fine, but I still find the same error when trying to run another migration, and a similar error, but instead of saying there's no attribute 'models', it says there's no attribute 'urls'

from django.contrib import admin
from .models import (Author, Book, Narrator, Audiobook)

# Register your models here.

admin.site.register(Author, Book)
admin.site.register(Narrator, Audiobook)
Derrick
  • 1
  • 2
  • 2
    This error is coming from the admin: you should show your admin.py. – Daniel Roseman Jan 13 '18 at 19:44
  • Good spot! I was registering my models incorrectly. I tried to register all in one line, but got an error that there were too many arguments, so I split it up into two lines, which felt wrong, and apparently is wrong. I still need to figure out how to register 4 models, but when I commented it all out, the migration worked. Thanks! – Derrick Jan 13 '18 at 20:06
  • Ok, the migration worked, but I run into errors if I try to import more than just the Book model. I probably am missing some steps and will slow down and go back to the documentation for the rest of it. Thanks so much for your help. – Derrick Jan 13 '18 at 20:22
  • If you have errors, you should post them (possibly in another question). For the admin file, if you want to register multiple models at once you need to put them in a list: `admin.site.register([Author, Book, Narrator, Audiobook])`. – Daniel Roseman Jan 13 '18 at 20:41
  • @DanielRoseman, your recommendation for putting the models in a list was exactly what I needed. `manage.py check` turned up no issues when I only imported the `Book` model, but kept returning the aforementioned error as soon as I tried importing the rest of the models. Consolidating the two `admin.site.register` lines into a single line with a list resolved the issue. Everything is now showing in the admin! Thanks for your help. – Derrick Jan 15 '18 at 04:30

2 Answers2

1

Try register models one by one like following:

admin.site.register(Author)
admin.site.register(Narrator)
admin.site.register(Book)
admin.site.register(Audiobook)
big pop
  • 51
  • 1
0

Documentation of Django is pretty easy to understand with examples.

Your admin.py must be similar to this :

from django.contrib import admin
from .models import Author, Book, Narrator, Audiobook
class AuthorAdmin(admin.ModelAdmin):
   fields = ['name']
class BookAdmin(admin.ModelAdmin):
   fields = ['title', 'description', ]

# Register your models here.

admin.site.register(Author, Book)
admin.site.register(Narrator, Audiobook)

you can check the basic example here to understand tutorial part 7.

ytsejam
  • 3,291
  • 7
  • 39
  • 69