Inline Changelists for Django
Project description
Inline Changelists for Django
Overview
Django Admin comes with inlines which allows you to display forms to edit related objects from the parent object’s change form. While it’s technically possible to use this infrastructure to display list of related objects, it gets hacky pretty fast.
Here’s where django-changelist-inline comes into play. It allows you to embed the standard list of objects as an inline on the parent object’s change form. Internally, it uses a subclass of ModelAdmin, which means you can customize the list as you’d normally do. Additionally, you can display links below the list of objects.
Usage Example
Let’s assume you have a Django project with two models - Thing and RelatedThing and that you want to display list of RelatedThing objects as an inline in Thing change form. The follwing snippet provides an example of how to implement that using django-changelist-inline:
# -*- coding: utf-8 -*-
from urllib.parse import urlencode
from django.contrib import admin
from django.urls import reverse
from django.utils.safestring import mark_safe
from django_changelist_inline import (
ChangelistInline,
ChangelistInlineAdmin,
ChangelistInlineModelAdmin,
)
from testing.models import RelatedThing, Thing
@admin.register(RelatedThing)
class RelatedThingModelAdmin(admin.ModelAdmin):
list_display = ('pk', 'name')
class RelatedThingChangelistInline(ChangelistInline):
model = RelatedThing
class ChangelistModelAdmin(ChangelistInlineModelAdmin):
list_display = ('name', 'format_actions')
list_display_links = None
list_per_page = 5
def get_queryset(self, request):
return RelatedThing.objects.filter(thing=self.parent_instance)
@mark_safe
def format_actions(self, obj=None):
if obj is None:
return self.empty_value_display
change_link_url = reverse(
'admin:{app_label}_{model_name}_change'.format(
app_label=RelatedThing._meta.app_label,
model_name=RelatedThing._meta.model_name,
),
args=[obj.pk],
)
return (
f'<a class="button" href="{change_link_url}">'
'Edit'
'</a>'
)
format_actions.short_description = 'Actions'
@property
def title(self):
return 'Linked Related Things'
@property
def no_results_message(self):
return 'No Related Things?'
def get_add_url(self, request):
result = super().get_add_url(request)
if result is not None:
return result + '?' + urlencode({
'thing': self.parent_instance.pk,
})
return result
def get_show_all_url(self, request):
result = super().get_show_all_url(request)
if result is not None:
return result + '?' + urlencode({
'thing': self.parent_instance.pk,
})
return result
def get_toolbar_links(self, request):
return (
'<a href="https://www.bthlabs.pl/">'
'BTHLabs'
'</a>'
)
@admin.register(Thing)
class ThingModelAdmin(ChangelistInlineAdmin):
inlines = (RelatedThingChangelistInline,)
API
ChangelistInline objects
The ChangelistInline class is the center piece of the API. It’s designed to be used in a ModelAdmin’s inlines.
In order for it to work, you’ll need to define the model property and embed ChangelistModelAdmin class, which should be a subclass of ChangelistInlineModelAdmin.
ChangelistInlineModelAdmin objects
The ChangelistInlineModelAdmin is a customized ModelAdmin subclass which provides sane defaults and additional functionality for inline changelists.
Changelist sanitization
This subclass overrides the following attributes and methods of ModelAdmin to provide sane defaults:
list_editable - set to empty tuple to disable editing of the list,
list_filter - set to empty tuple to disable filtering of the list,
search_fields - set to empty tuple to disable searching,
date_hierarchy - set to None,
sortable_by - set to empty tuple to disable sorting,
get_actions() - returns empty list to disable actions.
Additional functionality
To allow customization and to expose additional functionality, ChangelistInlineModelAdmin provides the following additional methods:
title property - returns the model’s verbose name by default.
no_results_message property - returns text to display in place of the table if no objects are fetched from the DB.
get_add_url(request) - returns URL for the model’s add form, if the user has the add permission. Return None to hide the add link.
get_show_all_url(request) - returns URL for the model’s changelist, if the user has the view permission. Return None to hide the show all link.
get_toolbar_links(request) - returns None by default. Override this to return string with additional <a/> elements to render in the toolbar. The return value is marked safe in the template.
ChangelistInlineAdmin objects
The ChangelistInlineAdmin class is a base class for ModelAdmin subclasses that use inline changelists.
ChangelistInlineAdminMixin
A mixin class that is used to properly configure changelist inlines in the parent ModelAdmin. Overrides get_inlines(request, obj=None) and get_inline_instances(request, obj=None) methods.
If you can’t use ChangelistInlineAdmin as you base class, you can use this mixin to enable inline changelists:
@admin.register(Thing)
class ThingModelAdmin(ChangelistInlineAdminMixin, MyBaseModelAdmin):
...
License
django-changelist-inline is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-changelist-inline-1.0.3.tar.gz
.
File metadata
- Download URL: django-changelist-inline-1.0.3.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.7.1 requests/2.26.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8e595a702211c702a4ca4e20ab5e43e566dc6b6e8182d26ef95887cd1a8654d |
|
MD5 | 75157bcc162531e58773b77e8dd5c221 |
|
BLAKE2b-256 | 6af7cf6bbbce9cb2b59a86d0ffb353f027cde6d1798933d871910ec310cc04bd |
File details
Details for the file django_changelist_inline-1.0.3-py3-none-any.whl
.
File metadata
- Download URL: django_changelist_inline-1.0.3-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.7.1 requests/2.26.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dd371b7374c766d91582fb847a7d302f010142636fe9f5728f0c468e7c26c20 |
|
MD5 | b0d8db7df2e58f2355455b4e3f6799a0 |
|
BLAKE2b-256 | ad44e5c970d9ea0cc727de14b00b3b9d28e7586f921ab35def19348bde2e8dda |