Opened 2 hours ago

Last modified 90 minutes ago

#35887 assigned Cleanup/optimization

Improve Examples for StackedInline and TabularInline

Reported by: Alexander Lazarević Owned by: Alexander Lazarević
Component: Documentation Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Alexander Lazarević)

I think the Django admin documentation could be improved by making the examples for StackedInline and TabularInline easier to incorporate. There are some bits and pieces missing sometimes that are not obvious.

Take the partial example to show how TabularInline is meant to be used:

from django.contrib import admin


class BookInline(admin.TabularInline):
    model = Book


class AuthorAdmin(admin.ModelAdmin):
    inlines = [
        BookInline,
    ]

So using this, you'll soon find out that the import for the Book model is missing. Easy enough to add this, but still a nuisance.

from .models import Book

Everything seems to work, but you can't see neither Authors nor Books in the admin, because the registration is missing. So you just quickly type in the code to register the ModelAdmins from your weak memory.

admin.register(Book, BookInline)
admin.register(Author, AuthorAdmin)

You realise the import for the Author model is missing as well. Ok, you just add that.

from .models import Author, Book

Everything looks fine, the server starts without any errors and again no Authors or Books in the admin.

You search through some other examples and see that you used admin.register instead of admin.site.register. After a face-palm you correct it.

admin.site.register(Book, BookInline)
admin.site.register(Author, AuthorAdmin)

Now the server puts out an error "AttributeError: 'BookInline' object has no attribute 'urls'".
You wonder what that's all about and after some time you find an example in the tutorial that only contains the registration for the not inlined ModelAdmin.

admin.site.register(Author, AuthorAdmin)

After that you got it finally working ...

I think providing the import of the models and the registration could prevent such on odyssey.

There are other places as well where some pieces are missing and it's not possible to just copy and paste the examples.

Change History (2)

comment:1 by Alexander Lazarević, 2 hours ago

Description: modified (diff)

comment:2 by Alexander Lazarević, 90 minutes ago

Has patch: set
Note: See TracTickets for help on using tickets.
Back to Top