|
| 1 | +============ |
| 2 | +Integrations |
| 3 | +============ |
| 4 | + |
| 5 | +django-polymorphic |
| 6 | +================== |
| 7 | + |
| 8 | +Quickstart |
| 9 | +---------- |
| 10 | + |
| 11 | +Support for `django-polymorphic`_ is currently in beta. To use polymorphic |
| 12 | +inlines with django-nested-admin follow the steps for |
| 13 | +`setting up django-polymorphic admin inline integration`_. The instructions |
| 14 | +are identical with django-nested-admin, except that the classes you would |
| 15 | +normally import from ``polymorphic.admin`` should instead be imported from |
| 16 | +``nested_admin`` and the classes themselves should be prefixed with |
| 17 | +``Nested`` For example: |
| 18 | + |
| 19 | +.. code-block:: python |
| 20 | +
|
| 21 | + # Instead of these imports |
| 22 | + from django.contrib.admin import ModelAdmin |
| 23 | + from polymorphic.admin import ( |
| 24 | + PolymorphicInlineSupportMixin, StackedPolymorphicInline) |
| 25 | +
|
| 26 | + # One should import: |
| 27 | + from nested_admin import ( |
| 28 | + NestedModelAdmin, |
| 29 | + NestedPolymorphicInlineSupportMixin, NestedStackedPolymorphicInline) |
| 30 | +
|
| 31 | +The polymorphic inlines used with django-nested-admin can have other inlines |
| 32 | +nested inside them, or even other polymorphic inlines. The only requirement |
| 33 | +is that all ModelAdmin and InlineAdmin classes inherit from the appropriate |
| 34 | +nested version. |
| 35 | + |
| 36 | +.. _django-polymorphic: https://django-polymorphic.readthedocs.io/en/stable/index.html |
| 37 | +.. _setting up django-polymorphic admin inline integration: https://django-polymorphic.readthedocs.io/en/stable/admin.html#inline-models |
| 38 | + |
| 39 | +Example |
| 40 | +------- |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + from django.contrib import admin |
| 45 | + import nested_admin |
| 46 | + from .models import ( |
| 47 | + Store, Customer, GuestCustomer, LoggedInCustomer, OrderItem, Product, WishListItem, |
| 48 | + Order, WishList, Payment, CreditCardPayment, BankPayment) |
| 49 | +
|
| 50 | + class PaymentInline(nested_admin.NestedStackedPolymorphicInline): |
| 51 | + model = Payment |
| 52 | + class CreditCardPaymentInline(nested_admin.NestedStackedPolymorphicInline.Child): |
| 53 | + model = CreditCardPayment |
| 54 | + class BankPayment(nested_admin.NestedStackedPolymorphicInline.Child): |
| 55 | + model = BankPayment |
| 56 | + child_inlines = (CreditCardPaymentInline, BankPayment) |
| 57 | +
|
| 58 | + class ProductInline(nested_admin.NestedStackedInline): |
| 59 | + model = Product |
| 60 | +
|
| 61 | + class WishListItemInline(nested_admin.NestedStackedInline): |
| 62 | + model = WishListItem |
| 63 | + sortable_field_name = 'position' |
| 64 | +
|
| 65 | + class WishListInline(nested_admin.NestedStackedInline): |
| 66 | + model = WishList |
| 67 | + inlines = [WishListItemInline] |
| 68 | +
|
| 69 | + class OrderItemInline(nested_admin.NestedStackedInline): |
| 70 | + model = OrderItem |
| 71 | +
|
| 72 | + class OrderInline(nested_admin.NestedTabularInline): |
| 73 | + model = Order |
| 74 | + inlines = [OrderItemInline, PaymentInline] |
| 75 | +
|
| 76 | + class CustomerInline(nested_admin.NestedStackedPolymorphicInline): |
| 77 | + model = Customer |
| 78 | + inlines = [OrderInline] |
| 79 | + class GuestCustomerInline(nested_admin.NestedStackedPolymorphicInline.Child): |
| 80 | + model = GuestCustomer |
| 81 | + class LoggedInCustomerInline(nested_admin.NestedStackedPolymorphicInline.Child): |
| 82 | + model = LoggedInCustomer |
| 83 | + inlines = [WishListInline] |
| 84 | + child_inlines = (GuestCustomerInline, LoggedInCustomerInline) |
| 85 | +
|
| 86 | + @admin.register(Store) |
| 87 | + class StoreAdmin(nested_admin.NestedPolymorphicModelAdmin): |
| 88 | + inlines = [CustomerInline] |
0 commit comments