Rewrite `admin.site.register()` to `@admin.register()`
See original GitHub issueDescription
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:
- Created a year ago
- Comments:9 (9 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just tested this against a client project, it rewrote a bunch of
admin.site.register
calls flawlessly, great work again @UnknownPlatypus .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.