[Answer]-Django admin – depending on radio button choice, show different fields

1👍

✅

You’ll need to add the additional field to your admin form class, then add some JavaScript to show or hide the appropriate field on page load, and also add an event handler to show the field to upload or paste in the link.

Both fields would need to allow blank=True and then you’d need to add a clean() method to make sure one of the fields is populated and then set the value appropriately. You might be better off using two separate fields.

You can leverage the Media inner class to easily add the JavaScript to the page without having to alter the change_form.html template for that app. Check out: https://docs.djangoproject.com/en/1.7/topics/forms/media/ for examples of how to add custom CSS and JavaScript to forms.

Leave a comment