Answer the question
In order to leave comments, you need to log in
How to remove code repetition in DJANGO?
I have many models, and in the admin panel in each admin.py I prescribe column formatting.
list_display = ('title', 'info_tag_created_at', 'info_tag_updated_at')
list_filter = ("languages", "is_view")
def info_tag_updated_at(self, obj):
if not obj.pk:
return ''
return obj.updated_at.strftime("%d.%m.%Y") + "<br/> <sup>" + obj.updated_at.strftime("%H:%M:%S") + "</sup>"
info_tag_updated_at.short_description = _('news.admin.info_tag_updated_at.short_description')
info_tag_updated_at.allow_tags = True
def info_tag_created_at(self, obj):
if not obj.pk:
return ''
return obj.created_at.strftime("%d.%m.%Y") + "<br/> <sup>" + obj.created_at.strftime("%H:%M:%S") + "</sup>"
info_tag_created_at.short_description = _('news.admin.info_tag_created_at.short_description')
info_tag_created_at.allow_tags = True
Answer the question
In order to leave comments, you need to log in
Make your base ModelAdmin and put the common for ALL models there.
Make some mixin classes to add some methods that only some models need.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question