How can I let a user select from a dropdown OR provide their own value in the Django admin?
I'm using a third-party app (Satchmo) and I want to change a text field to a dropdown with some preset options plus 开发者_如何学编程a text field where the user can fill in their own value.
I know how to mess with the admin model to change the field to use a dropdown of presets, but not how to preserve the users ability to specify the value themselves.
I suppose another option would be to use javascript to add a suggest feature to the text field, but I prefer the former.
Write a custom form field for it.
Documentation: http://code.djangoproject.com/wiki/CustomFormFields
Basically, you need to write methods to render, validate and convert post data.
You can define the javascript required to present the textbox when a particular dropdown value is chosen, and that script is also auto rendered by {{form.media}}
精彩评论