Support for inlines in Django Admin?
danmanix opened this issue · 1 comments
danmanix commented
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 commented
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()