Opened 2 years ago

Closed 19 months ago

Last modified 19 months ago

#19414 closed New feature (fixed)

Add a "register" class decorator for admin.

Reported by: stavros Owned by: BHold
Component: contrib.admin Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The syntax for registering an admin class is a bit unpythonic, so I propose that the admin.site.register method should also have the capability of being used as a decorator.

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".

Change History (12)

comment:1 Changed 2 years ago by julien

  • Component changed from Uncategorized to contrib.admin
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

comment:2 Changed 2 years ago by BHold

  • Owner changed from nobody to BHold

comment:3 Changed 2 years ago by BHold

  • Has patch set

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!

comment:4 follow-up: Changed 2 years ago by stavros

Why not just reuse admin.site.register? It looks like you reimplemented the method?

comment:5 in reply to: ↑ 4 Changed 2 years ago by BHold

Replying to 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 all be linked to the decorated ModelAdmin class in the Site's registry.

Last edited 2 years ago by BHold (previous) (diff)

comment:6 follow-up: Changed 2 years ago 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.

comment:7 in reply to: ↑ 6 Changed 2 years ago by BHold

Replying to 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?

comment:8 follow-up: Changed 2 years ago 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 pull request looks very good.

Last edited 2 years ago by stavros (previous) (diff)

comment:9 in reply to: ↑ 8 Changed 2 years ago by BHold

Replying to 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.

comment:10 Changed 2 years ago by rafales

I think you should also import this decorator in django/contrib/admin/__init__.py.

comment:11 Changed 19 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

In 98514849dce07acfaa224a90a784bba9d97249e5:

Fixed #19414 -- Added admin registration decorator

Thanks stavros for the suggestion.

comment:12 Changed 19 months ago by Tim Graham <timograham@…>

In e23de9e350d716c4d9ebe0b27c9f2752fe1aa543:

Fixed typo in exception message; refs #19414

Thanks Alexey Boriskin for the report.

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