Created
January 12, 2021 22:02
-
-
Save douglasmiranda/0c3ffbdb5c30185f9586f4261ae4ac64 to your computer and use it in GitHub Desktop.
Django Admin Inline / Access parent object/instance of an Inline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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