Reputation: 1
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)
Upvotes: 0
Views: 690
Reputation: 71
Try register models one by one like following:
admin.site.register(Author)
admin.site.register(Narrator)
admin.site.register(Book)
admin.site.register(Audiobook)
Upvotes: 1
Reputation: 3439
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.
Upvotes: 0