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

child models show in index regardless of show_in_index settings #532

Open
LeOndaz opened this issue Nov 9, 2022 · 1 comment
Open

child models show in index regardless of show_in_index settings #532

LeOndaz opened this issue Nov 9, 2022 · 1 comment

Comments

@LeOndaz
Copy link

LeOndaz commented Nov 9, 2022

from django.contrib import admin
from polymorphic.admin import PolymorphicParentModelAdmin, PolymorphicChildModelAdmin, PolymorphicChildModelFilter
from .models import Device, DeviceSubscribedEmail, Hub, Plug, PlugMini


class DeviceChildAdmin(PolymorphicChildModelAdmin):
    show_in_index = False

    def has_add_permission(self, request):
        return False

    def has_delete_permission(self, request, obj=None):
        return False


@admin.register(Plug)
class PlugAdmin(DeviceChildAdmin):
    base_model = Plug


class EmailSubscribersInline(admin.StackedInline):
    model = DeviceSubscribedEmail
    extra = 1


@admin.register(PlugMini)
class PlugMiniAdmin(DeviceChildAdmin):
    base_model = PlugMini
    fieldsets = [
        ("info", {"fields": ["id", "name", "country", "hub_device", "is_powered_on"]}),
        (
            "usage",
            {"fields": ["electricity_of_day", "voltage", "weight", "electric_current"]},
        ),
        ("thresholds", {"fields": ["weight_threshold"]}),
    ]
    inlines = [EmailSubscribersInline]


@admin.register(Hub)
class HubAdmin(DeviceChildAdmin):
    pass


@admin.register(Device)
class DeviceAdmin(PolymorphicParentModelAdmin):
    base_model = Device
    child_models = [PlugMini, Plug, Hub]
    list_filter = [PolymorphicChildModelFilter]

    def has_add_permission(self, request):
        return False

All models inherit from Device model which is a PolymorphicModel and still, all models show up in the admin page.

@nE0sIghT
Copy link

nE0sIghT commented Dec 2, 2022

Looks like duplicate of #497
See also workaround #497 (comment)

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