A simple implementation is below:
{{{
def register_admin(model):
def wrapper(klass):
admin.site.register(model, klass)
return klass
return wrapper
}}}
I feel that
{{{
@register(MyModel)
class AdminClass:
pass
}}}
is more pythonic than
{{{
class AdminClass:
pass
register(MyModel, AdminClass)
}}}
which just screams "decorator".
--
Ticket URL: <https://code.djangoproject.com/ticket/19414>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_docs: => 0
* component: Uncategorized => contrib.admin
* needs_tests: => 0
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:1>
* owner: nobody => BHold
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:2>
* has_patch: 0 => 1
Comment:
Added pull request: https://github.com/django/django/pull/599
I added a decorator that registers Model/ModelAdmin pairs with AdminSites.
I mostly followed how the AdminSite.register method currently does this.
Passes all tests, including those I added.
This is only my 2nd time contributing to the project, and first time
writing Sphinx docs, so please let me know if I didn't follow any
conventions!
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:3>
Comment (by stavros):
Why not just reuse admin.site.register? It looks like you reimplemented
the method?
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:4>
Comment (by BHold):
Replying to [comment:4 stavros]:
> Why not just reuse admin.site.register? It looks like you reimplemented
the method?
The decorator function should behave a little differently than the normal
register method of the Site. For one, it should accept a 'site' argument
so that a user can use it with a custom Site model. It also should be able
to accept multiple models as arguments that will be all be linked to the
decorated AdminClass in the Site's registry.
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:5>
Comment (by stavros):
Sure, but you don't have to reimplement the whole admin.site.register
method, you can just wrap it and add the extra functionality needed.
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:6>
Comment (by BHold):
Replying to [comment:6 stavros]:
> Sure, but you don't have to reimplement the whole admin.site.register
method, you can just wrap it and add the extra functionality needed.
True, I pushed up a new commit that does just that! What do you think?
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:7>
Comment (by stavros):
Much better! I would not have added the "Unsupported arguments" check if I
wrote it, just because someone might want to construct a dict with extra
stuff and pass it (plus, ignoring kwargs isn't bad for you), but otherwise
it looks great to me.
Can someone official chime in? This looks like a very good pull request.
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:8>
Comment (by BHold):
Replying to [comment:8 stavros]:
> Much better! I would not have added the "Unsupported arguments" check if
I wrote it, just because someone might want to construct a dict with extra
stuff and pass it (plus, ignoring kwargs isn't bad for you), but otherwise
it looks great to me.
>
> Can someone official chime in? This pull request looks very good.
Yeah, perhaps that Unsupported Arguments check was unneeded, I removed it.
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:9>
Comment (by rafales):
I think you should also import this decorator in
{{{django/contrib/admin/__init__.py}}}.
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:10>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"98514849dce07acfaa224a90a784bba9d97249e5"]:
{{{
#!CommitTicketReference repository=""
revision="98514849dce07acfaa224a90a784bba9d97249e5"
Fixed #19414 -- Added admin registration decorator
Thanks stavros for the suggestion.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:11>
Comment (by Tim Graham <timograham@…>):
In [changeset:"e23de9e350d716c4d9ebe0b27c9f2752fe1aa543"]:
{{{
#!CommitTicketReference repository=""
revision="e23de9e350d716c4d9ebe0b27c9f2752fe1aa543"
Fixed typo in exception message; refs #19414
Thanks Alexey Boriskin for the report.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/19414#comment:12>