[Fixed]-NameError: name 'BookAdmin' is not defined …Django

1👍

You are trying to register BookAdmin before actually creating it. Just move admin.site.register(Book, BookAdmin) after the class definition and it will work as expected.

class BookAdmin(admin.ModelAdmin):
    fieldsets = [
        (None,              {'fields': ['publication_date']}),
        ('Date infomation', {'fields': ['publisher']}),
    ]

admin.site.register(Book, BookAdmin)

0👍

It looks like you are trying to register BookAdmin before it’s definition. When the line admin.site.register(Book, BookAdmin) runs, the class BookAdmin hasn’t been defined yet.

Re-arrange the code slightly so you have:

class BookAdmin(admin.ModelAdmin):
    fieldsets = [
    (None,              {'fields': ['publication_date']}),
    ('Date infomation', {'fields': ['publisher']}),
    ]

admin.site.register(Book, BookAdmin)

Leave a comment