Related entities
Related-entity sections render independently paginated tables on the object
page, below the Django fieldsets (after_field_sets in
admin/chassis/change_form.html). Each section has a provider method
marked with @chassis_related_items. Field tables:
Options catalog.
from django.http import HttpRequest
from django.utils.translation import gettext_lazy as _
from django_chassis.decorators import chassis_related_items
from django_chassis.options import RelatedEntitiesOption, RelatedEntityOption, TableColumnOption, TableOption
class AuthorAdmin(ChassisAdminMixin, ModelAdmin):
options = [
RelatedEntitiesOption(
sections=[
RelatedEntityOption(
slug='books',
title=_('Books'),
get_items_method='get_books',
page_size=10,
table=TableOption(
columns=[
TableColumnOption(field='title', label=_('Title')),
TableColumnOption(field='isbn', label=_('ISBN')),
]
),
)
]
)
]
@classmethod
@chassis_related_items
def get_books(cls, request: HttpRequest, obj: Author):
return obj.books.all()
RelatedEntitiesOption
| Parameter | Type | Default |
|---|---|---|
sections | list[RelatedEntityOption] | required |
fieldset_title | localized string | Related entities |
collapsed | bool | False |
pagination_state_ttl_seconds | int | 30 * 60 |
RelatedEntityOption
| Parameter | Type | Default |
|---|---|---|
slug | unique section slug | required |
title | localized string | required |
get_items_method | method name | required |
table | TableOption | required |
page_size | int | None | None uses 10 items |
collapsed | bool or None | inherit parent |
permission | codename or None | None — require the given permission to view this section when set |
The provider must be a @classmethod decorated with
@chassis_related_items and return a queryset or a sequence. Pagination is
independent per section. The footer shows the total count on the left
and numbered pages on the right.
See also
Editable related records
Changed in 1.0.23For example, let staff edit a product's suppliers in the product card. In this
example Supply.product and Supply.supplier are foreign keys, and
Supply.sku is a scalar field. Register SupplierAdmin on the same site and
configure search_fields or SearchOption for its supplier name.
from django.contrib.admin import ModelAdmin
from django_chassis.mixins import ChassisAdminMixin
from django_chassis.options import RelatedCollectionOption, RelatedCollectionsOption
from catalog.models import Supply
class ProductAdmin(ChassisAdminMixin, ModelAdmin):
options = [
RelatedCollectionsOption(collections=[
RelatedCollectionOption(
model=Supply,
foreign_key='product',
fields=['supplier', 'sku'],
autocomplete_fields=['supplier'],
extra=1,
min_num=0,
max_num=10
)
])
]
Use fields for a multi-column editor and value_field for the compact
single-field editor. With fields, an optional field_classes mapping
overrides form field classes for selected names. To validate a combination,
pass a ModelForm subclass via form. Chassis applies Django inline
permissions for the child model. Test with a staff user who has the intended
child permissions. Saving an invalid child or exceeding the row limit shows
a form error.
For a foreign key on the main form, declare
EntityPickerOption(fields=['category']). The related ModelAdmin must be
registered on the same site and support search. To restrict selectable
categories, filter its get_queryset(request). Chassis applies that
queryset again when validating the submitted foreign key, so a manually
submitted ID outside it fails validation.