question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rewrite `admin.site.register()` to `@admin.register()`

See original GitHub issue

Description

Description

We could rewrite admin.site.register() to the more elegant @admin.register() syntax :

+@admin.register(MyModel)
class MyCustomAdmin:
    pass

-admin.site.register(MyModel, MyCustomAdmin)

This syntax was actually added a while ago (Django1.7). I usually find it a bit easier to use since the Model and associated Custom Admin definition stay really close.

Potential Issue

There is a known issue when using a python 2 style super call in the __init__ method. The fixer should not rewrite in this case.

You can’t use this decorator if you have to reference your model admin class in its __init__() method, e.g. super(PersonAdmin, self).__init__(*args, **kwargs). You can use super().__init__(*args, **kwargs).

Let me know if this is something you’ll be interested in, I’ll be happy to submit a PR. Cheers

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
adamchainzcommented, Sep 14, 2022

Just tested this against a client project, it rewrote a bunch of admin.site.register calls flawlessly, great work again @UnknownPlatypus .

1reaction
adamchainzcommented, Sep 12, 2022

Okay the main fixer was added in #189 , thank you! 👏👏👏

I’m not sure about supporting the extra arguments. It’s not 100% safe to modify the main class, since Django creates a subclass and so the attribute “isn’t there” in the main class. There could be several registered classes spread between files and rewriting this case would affect them all.

I’ve opened #190 with the further cases that I think are safe enough to support. Let’s continue the discussion there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Django Tutorial Part 4: Django admin site - MDN Web Docs
First we'll show you how to register the models with the admin site, then we'll show you how to login and create some...
Read more >
The Django admin site
ModelAdmin ): pass admin.site.register(Author, AuthorAdmin). Do you need a ModelAdmin ... class for the admin. It calls autodiscover() when Django starts.
Read more >
Django: Admin register decorator for custom admin site class
You can use the keyword argument site like this: @admin.register(MyModel, site=MyAdminSite) from django.contrib import admin ... @admin.register(MyModel ...
Read more >
Customize the Django Admin With Python
ModelAdmin ): pass @admin.register(Course) class CourseAdmin(admin. ... By default, the Django admin displays each object by calling str() on it.
Read more >
Admin integration — django-reversion 5.0.4 documentation
If you've registered your models using reversion.register(), the admin class will use the configuration you specify there. Otherwise, the admin class will ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found