Working on the Django tutorial “Writing your own Django app,” and I’m on Part 2.
Midway through, it instructs me to add a line to the admin so that the admin will recognize not just Poll (and PollAdmin, which the tutorial has been configuring for some custom poll-presenting options), but also Choice. Here’s the (short) updated admin.py:
from polls.models import Poll
from polls.models import Choice
from django.contrib import admin
class PollAdmin(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['question']}),
('Date information', {'fields': ['pub_date'], 'classes': ['collapse']}),
]
admin.site.register(Poll, PollAdmin)
admin.site.register(Choice)
Looking at this, I can’t figure out why I can’t simply write
admin.site.register(Poll, PollAdmin, Choice)
except that this gives me a TypeError, because
register() takes at most 3 arguments (4 given)
This seems really… arbitrary to me. I don’t understand why register only takes at most 3 arguments. My understanding of Django is still at a very voodoo, cargo-cult level, so I get that this Just. Doesn’t. Work., but I was wondering if some light could be shed on why I can’t pull all three elements from admin.site at the same time.
Because you’ve got two separate types of registration going on here, with two separate types of objects. I think you’re thinking that this is saying “register these three things with the admin”, but that’s not quite what’s going on.
The first line is saying “register the Poll model with the admin, using the PollAdmin class I’ve defined.”
The second line is saying “register the Choice model with the admin, using the default settings.”
So it wouldn’t make sense to have them all in the same line. You’re only registering one model at a time, but one uses some explicit options, and the other uses the default options.