adminextensions.shortcuts

serialized_many_to_many_field

The serialized_many_to_many_field shows the contents of a many-to-many relation inline in the admin change list:

# in app/admin.py

from app.models import Author, Genre

from adminextensions import ExtendedModelAdmin
from adminextensions.shortcuts import serialized_many_to_many_field
from django.contrib import admin

class AuthorAdmin(ExtendedModelAdmin):
    list_display = (
        'given_name', 'family_name',
        serialized_many_to_many_field('genre')
    )

admin.site.register(Author, AuthorAdmin)
admin.site.register(Genre)

The list of models is just plain text by default. Links to the models can be printed instead, by supplying linked=True to serialized_many_to_many_field.

The destination models are displayed using their __unicode__ method by default. This can be overridden by supplying a callable as the formatter kwarg. This should accept a single argument, which is the model instance:

class AuthorAdmin(ExtendedModelAdmin):
    list_display = (
        'given_name', 'family_name',
        serialized_many_to_many_field('genre',
                                      formatter=lambda g: g.name)
    )

Items in the list are joined by ', ' by default. This can be overridden using the joiner kwarg.

A short_description parameter is automatically generated based on the linked field name. To override this, use the short_description parameter:

class AuthorAdmin(ExtendedModelAdmin):
    list_display = (
        'given_name', 'family_name',
        serialized_many_to_many_field('genre', short_description='writes')
    )

truncated_field

The truncated_field shows a truncated version of a field. Use this on content fields that may have a lot of data. The data is truncated after length words. length defaults to 20:

# in app/admin.py

from app.models import Author, Genre

from adminextensions import ExtendedModelAdmin
from adminextensions.shortcuts import truncated_field
from django.contrib import admin

class BookAdmin(ExtendedModelAdmin):
    list_display = (
        'title', truncated_field('content', length=15),
    )

admin.site.register(Book, BookAdmin)

A short_description parameter is automatically generated based on the linked field name. To override this, use the short_description parameter.

Project Versions

Table Of Contents

Previous topic

Usage

This Page