Created
October 20, 2021 11:35
-
-
Save ihadgraft/d0ddd785edc4d6eb44813bd7e0ea3c87 to your computer and use it in GitHub Desktop.
Setting the initial value for a field on an inline with Django admin
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
""" | |
This examples shows setting the current user as the default value for a field named 'user' | |
""" | |
class MyInline(admin.TabularInline): | |
def formfield_for_dbfield(self, db_field, request, **kwargs): | |
field = super().formfield_for_dbfield(db_field, request, **kwargs) | |
if db_field.name == 'user': | |
field.initial = request.user | |
return field |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment