Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default for dropdown menu in Django admin

I'm using Django 1.6. I am trying to work out whether there is a way to set a default for the option that appears in a dropdown box on the admin site (rather than the blank ------ option that appears). Specifically I am working with dropdown boxes for foreign keys, so they have no "choice" field.

like image 540
kmc Avatar asked Sep 03 '26 13:09

kmc


1 Answers

Dropdown default can be set by specifying it in the foreign key field in models.py :

foreign_key_field = models.ForeignKey("keyname", default=1)

This will select the record with id=1 in the corresponding admin dropdown by default. You may change default=1 to any id that you want to show.

like image 102
obayhan Avatar answered Sep 06 '26 03:09

obayhan