Skip to main content

Related entities

Added in 1.0.1

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​

ParameterTypeDefault
sectionslist[RelatedEntityOption]required
fieldset_titlelocalized stringRelated entities
collapsedboolFalse
pagination_state_ttl_secondsint30 * 60

RelatedEntityOption​

ParameterTypeDefault
slugunique section slugrequired
titlelocalized stringrequired
get_items_methodmethod namerequired
tableTableOptionrequired
page_sizeint | NoneNone uses 10 items
collapsedbool or Noneinherit parent
permissioncodename or NoneNone — 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.

Changed in 1.0.23

See also​

Changed in 1.0.23

For 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.

Added in 1.0.23

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.