-
Notifications
You must be signed in to change notification settings - Fork 2
User Guide
from django.apps import apps
from django.contrib import admin
from admin_extended.base import ExtendedAdminModel
from . import models
class PostCommentInline(admin.TabularInline):
model = models.PostComment
extra = 0
class PostTagInline(admin.TabularInline):
model = models.PostTag
extra = 0
@admin.register(models.Post)
class PostAdmin(ExtendedAdminModel):
list_display = ('id', 'title', 'post_at')
search_fields = ('title',)
search_help_text = 'Search by title'
list_filter = ('status',)
inlines = [
PostCommentInline,
PostTagInline
]
ExtendedAdminModel options
- raw_id_fields_as_default (boolean, default True) Use raw_id_fields (or autocomplete_fields if related model have search_fields) as default for ForeginKey instead of select box (optimize performance for large database)
- delete_without_confirm (boolean, default False) Ignore confirm page for delete action
- tab_inline (boolean, default from setting) Use tab for model inline (override value in setting)
- super_admin_only_fields (list, default []) Only show these fields if user login is superuser
-
ext_read_only_fields (list, default []) Only show these fields in view mode. Default custom fields start with
display_
was mark as read only so you don't need add these fields to ext_read_only_fields - ext_write_only_fields (list, default []) Only show these fields in edit mode
- enable_foreign_link (boolean, default True) Add link for foregin key in change list page
Use can use DefaultModelAdmin
to quickly define model admin for model. This class will auto register all field in model to list_display (ignore TextField, JsonField by default), auto add to list_filter for choice fields
@admin.register(models.Player)
class PlayerAdmin(DefaultModelAdmin):
list_display_ignore_field_name = ("access_token", "gid", "wallet_id")
Add this code at end of admin.py file of lastest install app (INSTALLED_APPS setting)
from admin_extended.utils import auto_register_model_admin
auto_register_model_admin()
auto_register_model_admin(default_model_admin_class=DefaultModelAdmin, ignore_models=[]): This function will automatic register admin for all unregistered model
- default_model_admin_class: DefaultModelAdmin will list all fields (exclude TextField) of model in change list page, you can custom your model admin and pass to this param
- ignore_models: list model you don't want auto register. specify by <app_label>.<model_name>. Eg: 'users.user'
Add custom object tools item in change list or change form
from admin_extended.decorators import object_tool
from admin_extended.base import ExtendedAdminModel
@admin.register(models.Customer)
class CustomerAdmin(ExtendedAdminModel):
change_form_object_tools = ['demo_change_form_action']
change_list_object_tools = ['demo_change_list_action']
@object_tool(icon='fas fa-edit', name='do_something', description='Do something', http_method='post', post_param_title='Name')
def demo_change_form_action(self, request, object_id):
customer = models.Customer.objects.get(pk=object_id)
context = {
**admin.site.each_context(request),
'title': f'Update customer {customer.name}',
}
if request.method == 'POST':
form = CustomForm(request.POST)
messages.success(request, request.POST.get('data'))
if form.is_valid():
print(form.cleaned_data)
return redirect(reverse('admin:shop_customer_change', args=[object_id]))
context["form"] = CustomForm()
return render(request, 'admin/custom/custom_form.html', context)
@object_tool(icon='fas fa-edit', name='demo_change_list_action', description='Do something')
def demo_change_list_action(self, request):
context = {
**admin.site.each_context(request),
'title': f'Import customer',
}
if request.method == 'POST':
form = CustomForm(request.POST)
if form.is_valid():
print(form.cleaned_data)
return redirect(reverse('admin:shop_customer_changelist'))
context["form"] = CustomForm()
return render(request, 'admin/custom/custom_form.html', context)
*object_tool(function=None, , icon=None, name=None, description=None, http_method='get', post_param_title=None)
- icon: icon of button
- name: name of object tool (must unique)
- description: label of button
- http_method: Only affect in change form page. with http_method is post, you can pass one param when submit object tool.
- post_param_title: Only affect when http_method is post. Title of param you want to pass.
Result
- Go to page you want add to bookmark
- Click bookmark button add bottom right
- Choose name of bookmark
- You also can manage book mark (add, delete, change order, ...) in bookmark model
This app provides an easy way to create bar and line charts in Django Admin.
Example with model Rating
in app shop
class Rating(models.Model):
customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name='ratings')
value = models.IntegerField()
comment = models.TextField(blank=True, null=True)
created_at = models.DateTimeField()
def __str__(self):
return str(self.id)
We can go to django admin and define chart to visualize rating count by time, like this
Result