Reputation: 37846
I am trying to do this cool stuff in django admin
I have a model which has a ImageField. What I want is that the my other coworkers are able to decide if they want to upload image or just paste the image link.
like
<radio>: upload <radio>: link
and depending on choice, respective field (which is model's imageField) will show up for under these radio buttons.
how can I achieve this in django admin?
Upvotes: 0
Views: 970
Reputation: 34553
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.
Upvotes: 1