{"id":671,"date":"2019-08-18T13:08:32","date_gmt":"2019-08-18T12:08:32","guid":{"rendered":"http:\/\/watchdiana.fail\/blog\/?p=671"},"modified":"2019-08-19T09:57:25","modified_gmt":"2019-08-19T08:57:25","slug":"subclassing-django-models-in-the-admin","status":"publish","type":"post","link":"https:\/\/watchdiana.fail\/blog\/2019\/08\/18\/subclassing-django-models-in-the-admin\/","title":{"rendered":"Proxy Django models for the admin"},"content":{"rendered":"<p>in admin.py you cannot register an admin class, and you can only register an imported class once.  You CAN register an altered class, but that is an overload on the original registration, so<\/p>\n<p><code>admin.register(MyClass)<br \/>\nadmin.register(MyClass)  # Will throw an error and a hissy fit.<\/code><\/p>\n<p>However, it is possible to create a proxied class if, for example, you want to have a full set of that class&#8217;s attributes and linked classes, and a simple version where you do not load the full set.  In a database I maintain, the linked child classes take a long time to load, and are not always necessary.<\/p>\n<p><code># In models.py<br \/>\nclass CharacterProxy(Character):<br \/>\n&nbsp;<br \/>\n<\/code><code>&nbsp;&nbsp;&nbsp;&nbsp;class Meta:<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;proxy = True<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;verbose_name_plural = \"Shorter Character Sheet\"<br \/>\n&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br \/>\n# In admin.py<br \/>\n&nbsp;<br \/>\n# define fieldsets within an AlterCharacter class if you want to reduce them.<br \/>\nadmin.register(Character)<br \/>\nadmin.register(CharacterProxy)<\/code><\/p>\n<p>You can also use a register decorator:<br \/>\n<code><br \/>\n@admin.register(Character)<br \/>\nclass CharacterAdmin(admin.ModelAdmin):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;list_display = (\"name\", \"species\")<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;...<\/code><\/p>\n<p><code><br \/>\n<\/code><code>@admin.register(CharacterProxy)<br \/>\nclass CharacterProxyAdmin(admin.ModelAdmin):<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;search_fields = [\"name\"]<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;...<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>in admin.py you cannot register an admin class, and you can only register an imported class once. You CAN register an altered class, but that is an overload on the original registration, so admin.register(MyClass) admin.register(MyClass) # Will throw an error and a hissy fit. However, it is possible to create a proxied class if, for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[83,104,105,48],"class_list":["post-671","post","type-post","status-publish","format-standard","hentry","category-life","tag-fun-with-code","tag-fun-with-django","tag-fun-with-python","tag-things-i-do-because-im-learning","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts\/671","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/comments?post=671"}],"version-history":[{"count":13,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts\/671\/revisions"}],"predecessor-version":[{"id":684,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts\/671\/revisions\/684"}],"wp:attachment":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/media?parent=671"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/categories?post=671"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/tags?post=671"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}