Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for inlines in Django Admin? #69

Open
danmanix opened this issue Feb 11, 2023 · 1 comment
Open

Support for inlines in Django Admin? #69

danmanix opened this issue Feb 11, 2023 · 1 comment

Comments

@danmanix
Copy link

Haven't been able to dig through Admin to find out how to apply the same kind of patching to inline objects as is done in the included ModelAdmin subclass.

If anyone has any pointers would be happy to try further.

@mcosti
Copy link

mcosti commented Sep 20, 2023

This is what I use in my project

class TabularInlineTypedModel(admin.TabularInline):
    """Mixin copied typedmodels.admin.TypedModelAdmin"""

    def get_fields(self, request, obj=None):
        fields = super().get_fields(request, obj)
        # we remove the type field from the admin of subclasses.
        if TypedModel not in self.model.__bases__:
            fields.remove(self.model._meta.get_field("type").name)
        return fields

    def save_model(self, request, obj, form, change):
        if getattr(obj, "_typedmodels_type", None) is None:
            # new instances don't have the type attribute
            obj._typedmodels_type = form.cleaned_data["type"]
        obj.save()


class StackedInlineTypedModel(admin.StackedInline):
    """Mixin copied typedmodels.admin.TypedModelAdmin"""

    def get_fields(self, request, obj=None):
        fields = super().get_fields(request, obj)
        # we remove the type field from the admin of subclasses.
        if TypedModel not in self.model.__bases__:
            fields.remove(self.model._meta.get_field("type").name)
        return fields

    def save_model(self, request, obj, form, change):
        if getattr(obj, "_typedmodels_type", None) is None:
            # new instances don't have the type attribute
            obj._typedmodels_type = form.cleaned_data["type"]
        obj.save()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants