Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Extra features for django.contrib.admin

Project description

Installation

For installing graphene, just run this command in your shell

pip install django-admin-extras

Settings

INSTALLED_APPS = (
    # ...
    'django_admin_extras',
    # ...
)

Examples

Here is a simple Django model:

from django.db import models


class TodoList(models.Model):
    title = models.CharField(max_length=64)

    def __str__(self):
        return self.title


class TodoItem(models.Model):
    text = models.CharField(max_length=256)
    checked = models.BooleanField(default=False)
    list = models.ForeignKey(TodoList, on_delete=models.CASCADE)

    def __str__(self):
        return self.text

Here is a simple Django admin for models above:

from django.contrib import admin
from django.db.models import Q

from django_admin_extras import InputFilter, custom_titled_filter, custom_view_field

from .models import TodoItem


class TodoItemTextFilter(InputFilter):
    parameter_name = 'todoitem__text'
    title = 'todo item text'

    def queryset(self, request, queryset):
        if self.value() is not None:
            q = Q()
            for text_part in self.value().split():
                q &= Q(text__icontains=text_part)
            return queryset.filter(q)


@admin.register(TodoItem)
class TodoItemAdmin(admin.ModelAdmin):
    list_display = 'text', 'custom_text', 'checked', 'custom_bool',
    list_filter = TodoItemTextFilter, ('checked', custom_titled_filter('test title for checked filter')),

    @custom_view_field(admin_order_field='-text')
    def custom_text(self, obj: TodoItem) -> str:
        return obj.text + ' custom'

    @custom_view_field(boolean=True, short_description='Not checked')
    def custom_bool(self, obj: TodoItem) -> bool:
        return not obj.checked

Project details


Release history Release notifications

This version

1.0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for django-admin-extras, version 1.0.2
Filename, size File type Python version Upload date Hashes
Filename, size django_admin_extras-1.0.2-py3-none-any.whl (5.8 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size django-admin-extras-1.0.2.tar.gz (4.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page