Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generic.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. from django.contrib.contenttypes.admin import GenericInlineModelAdmin
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.utils.functional import cached_property
  4. from polymorphic.formsets import (
  5. BaseGenericPolymorphicInlineFormSet,
  6. GenericPolymorphicFormSetChild,
  7. polymorphic_child_forms_factory,
  8. )
  9. from .inlines import PolymorphicInlineModelAdmin
  10. class GenericPolymorphicInlineModelAdmin(PolymorphicInlineModelAdmin, GenericInlineModelAdmin):
  11. """
  12. Base class for variation of inlines based on generic foreign keys.
  13. """
  14. #: The formset class
  15. formset = BaseGenericPolymorphicInlineFormSet
  16. def get_formset(self, request, obj=None, **kwargs):
  17. """
  18. Construct the generic inline formset class.
  19. """
  20. # Construct the FormSet class. This is almost the same as parent version,
  21. # except that a different super is called so generic_inlineformset_factory() is used.
  22. # NOTE that generic_inlineformset_factory() also makes sure the GFK fields are excluded in the form.
  23. FormSet = GenericInlineModelAdmin.get_formset(self, request, obj=obj, **kwargs)
  24. FormSet.child_forms = polymorphic_child_forms_factory(
  25. formset_children=self.get_formset_children(request, obj=obj)
  26. )
  27. return FormSet
  28. class Child(PolymorphicInlineModelAdmin.Child):
  29. """
  30. Variation for generic inlines.
  31. """
  32. # Make sure that the GFK fields are excluded from the child forms
  33. formset_child = GenericPolymorphicFormSetChild
  34. ct_field = "content_type"
  35. ct_fk_field = "object_id"
  36. @cached_property
  37. def content_type(self):
  38. """
  39. Expose the ContentType that the child relates to.
  40. This can be used for the ``polymorphic_ctype`` field.
  41. """
  42. return ContentType.objects.get_for_model(self.model, for_concrete_model=False)
  43. def get_formset_child(self, request, obj=None, **kwargs):
  44. # Similar to GenericInlineModelAdmin.get_formset(),
  45. # make sure the GFK is automatically excluded from the form
  46. defaults = {"ct_field": self.ct_field, "fk_field": self.ct_fk_field}
  47. defaults.update(kwargs)
  48. return super(GenericPolymorphicInlineModelAdmin.Child, self).get_formset_child(
  49. request, obj=obj, **defaults
  50. )
  51. class GenericStackedPolymorphicInline(GenericPolymorphicInlineModelAdmin):
  52. """
  53. The stacked layout for generic inlines.
  54. """
  55. #: The default template to use.
  56. template = "admin/polymorphic/edit_inline/stacked.html"