Django Admin Page

January 24th, 2020   プログラミング  Django

I wanted to change the list of items in the Post model on the Django Admin page. I created a class in admin.py and tried to register the model, but I couldn't register. The cause was that registration was duplicated because the Post model had already been registered with Markdownx.

I want to use Markdownx, so I checked for other methods. There was a way to inherit and override the model by "proxy model".

There were other methods such as creating a custom template, but it was good that using proxy model was easy way.

Reference sites


Reference codes


model.py

  1. # Admin画面の項目並びで利用
  2. class PostProxy(Post):
  3.  
  4. class Meta:
  5. proxy = True
  6.  

admin.py

  1. """
  2. class PostAdmin(admin.ModelAdmin):
  3. list_display = ("title", "title_en", "category", "published_date")
  4. """
  5.  
  6. admin.site.register(Category)
  7. admin.site.register(Tag)
  8. """ admin.site.register(Post, PostAdmin) """
  9. admin.site.register(Post, MarkdownxModelAdmin)
  10. admin.site.register(Comment)
  11.  
  12. @admin.register(PostProxy)
  13. class PostProxyAdmin(admin.ModelAdmin):
  14. list_display = ("title", "title_en", "category", "published_date")
  15.  

Reference image


Sponsor Link