Skip to content

Instantly share code, notes, and snippets.

@ihadgraft
Created October 20, 2021 11:35
Show Gist options
  • Save ihadgraft/d0ddd785edc4d6eb44813bd7e0ea3c87 to your computer and use it in GitHub Desktop.
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 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