Skip to content

Instantly share code, notes, and snippets.

@riklomas
Created July 9, 2009 10:27
Show Gist options
  • Save riklomas/143562 to your computer and use it in GitHub Desktop.
Save riklomas/143562 to your computer and use it in GitHub Desktop.
How to default (or initial) an admin select in Django's ModelAdmin
def formfield_for_dbfield(self, db_field, **kwargs):
if db_field.name == "user":
return forms.ModelChoiceField(queryset=User.objects.all().order_by('username'), initial=kwargs['request'].user.id)
return super(NamedAdmin, self).formfield_for_dbfield(db_field, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment