Debian Codegraph @

src/polymorphic/admin/generic.py

raw
1 from __future__ import annotations
2
3 from typing import Any, cast
4
5 from django.contrib.contenttypes.admin import GenericInlineModelAdmin
6 from django.contrib.contenttypes.models import ContentType
7 from django.http import HttpRequest
8 from django.utils.functional import cached_property
9
10 from polymorphic.formsets import (
11 BaseGenericPolymorphicInlineFormSet,
12 GenericPolymorphicFormSetChild,
13 PolymorphicFormSetChild,
14 polymorphic_child_forms_factory,
15 )
16
17 from .inlines import PolymorphicInlineModelAdmin
18
19
20 class GenericPolymorphicInlineModelAdmin(PolymorphicInlineModelAdmin, GenericInlineModelAdmin):
21 """
22 Base class for variation of inlines based on generic foreign keys.
23 """
24
25 #: The formset class
26 formset: type[BaseGenericPolymorphicInlineFormSet] = BaseGenericPolymorphicInlineFormSet # type: ignore[assignment]
27
28 def get_formset( # type: ignore[override]
29 self, request: HttpRequest, obj: Any = None, **kwargs: Any
30 ) -> type[BaseGenericPolymorphicInlineFormSet]:
31 """
32 Construct the generic inline formset class.
33 """
34 # Construct the FormSet class. This is almost the same as parent version,
35 # except that a different super is called so generic_inlineformset_factory() is used.
36 # NOTE that generic_inlineformset_factory() also makes sure the GFK fields are excluded in the form.
37 FormSet = GenericInlineModelAdmin.get_formset(self, request, obj=obj, **kwargs)
38
39 setattr(
40 FormSet,
41 "child_forms",
42 polymorphic_child_forms_factory(
43 formset_children=self.get_formset_children(request, obj=obj)
44 ),
45 )
46 return cast(type[BaseGenericPolymorphicInlineFormSet], FormSet)
47
48 class Child(PolymorphicInlineModelAdmin.Child):
49 """
50 Variation for generic inlines.
51 """
52
53 # Make sure that the GFK fields are excluded from the child forms
54 formset_child: type[GenericPolymorphicFormSetChild] = GenericPolymorphicFormSetChild
55 ct_field: str = "content_type"
56 ct_fk_field: str = "object_id"
57
58 @cached_property
59 def content_type(self) -> ContentType:
60 """
61 Expose the ContentType that the child relates to.
62 This can be used for the ``polymorphic_ctype`` field.
63 """
64 return ContentType.objects.get_for_model(self.model, for_concrete_model=False)
65
66 def get_formset_child(
67 self, request: HttpRequest, obj: Any = None, **kwargs: Any
68 ) -> PolymorphicFormSetChild:
69 # Similar to GenericInlineModelAdmin.get_formset(),
70 # make sure the GFK is automatically excluded from the form
71 defaults = {"ct_field": self.ct_field, "fk_field": self.ct_fk_field}
72 defaults.update(kwargs)
73 return super(GenericPolymorphicInlineModelAdmin.Child, self).get_formset_child(
74 request, obj=obj, **defaults
75 )
76
77
78 class GenericStackedPolymorphicInline(GenericPolymorphicInlineModelAdmin):
79 """
80 The stacked layout for generic inlines.
81 """
82
83 #: The default template to use.
84 template: str = "admin/polymorphic/edit_inline/stacked.html"