-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
48 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,36 @@ | ||
How to add a model twice to Django admin? | ||
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
+++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
|
||
You need to add the :code:`Hero` model twice to the admin, one as a regular admin area, and one as read only admin. (SOme user will potentially see only the read only admin.) | ||
|
||
If you have try to register the same model twice:: | ||
|
||
admin.site.register(Hero) | ||
admin.site.register(Hero) | ||
|
||
you will get an error like this:: | ||
|
||
raise AlreadyRegistered('The model %s is already registered' % model.__name__) | ||
|
||
THe solution is to sublass the :code:`Hero` model as a ProxyModel.:: | ||
|
||
# In models.py | ||
class HeroProxy(Hero): | ||
|
||
class Meta: | ||
proxy = True | ||
|
||
... | ||
# In admin.py | ||
@admin.register(Hero) | ||
class HeroAdmin(admin.ModelAdmin, ExportCsvMixin): | ||
list_display = ("name", "is_immortal", "category", "origin", "is_very_benevolent") | ||
.... | ||
|
||
|
||
@admin.register(HeroProxy) | ||
class HeroProxyAdmin(admin.ModelAdmin): | ||
readonly_fields = ("name", "is_immortal", "category", "origin", | ||
...) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters