Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created January 12, 2021 22:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasmiranda/0c3ffbdb5c30185f9586f4261ae4ac64 to your computer and use it in GitHub Desktop.
Save douglasmiranda/0c3ffbdb5c30185f9586f4261ae4ac64 to your computer and use it in GitHub Desktop.
Django Admin Inline / Access parent object/instance of an Inline
# Let's say you want to access the Collection instance for a Image in a Django Admin Inline
from django.contrib import admin
from app.models import Image, Collection
class ImageInline(admin.StackedInline):
model = models.Image
extra = 1
def get_formset(self, request, obj=None, **kwargs):
# If you want to access the Collection instance
# register here
self.parent_inline_obj = obj # Collection instance
return super().get_formset(request, obj, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment