Issue
I'm using a formset with can_delete=True. I want to change the widget of the DELETE field to a hidden input. I can't seem to find a good way to do this. What I've tried is:
Change the form's widget to HiddenInput and/or add a hidden field in the form definition:
class MyForm(ModelForm):
DELETE = forms.BooleanField(widget=forms.HiddenInput)
class Meta:
model = MyModel
widgets = {'DELETE' : forms.HiddenInput}
Do the above with a change in the formset
class MyFormSet(BaseModelFormSet):
def add_fields(self, form, index):
originalDeletion = None
if DELETION_FIELD_NAME in form.fields:
originalDeletion = form.fields[DELETION_FIELD_NAME]
super(MyFormSet, self).add_fields(form,index)
if originalDeletion is not None:
form.fields[DELETION_FIELD_NAME] = originalDeletion
If I do these both it does actually change the field to hidden but this seems like a bit of a hack (effectively overwriting the usual add_fields method). How are you supposed to do this?
== EDIT ==
It turns out that using a hidden field is not so good with the form framework anyway. You should definitely use a checkbox and hide it with css. If you want to do adjust the css of the checkbox in Django I still think you have to change the add_fields method as above, which then allows you to change the widget's css.
Solution
It doesn't change anything if your input is of type=hidden, or if it is of type=checkbox and display: none.
IMHO the elegant way in CSS would look like this:
td.delete input { display: none; }
Or in JavaScript:
$('td.delete input[type=checkbox]').hide()
Or, in the admin:
django.jQuery('td.delete input[type=checkbox]').hide()
That seems like a better direction to take because:
- It won't change your JavaScript code and
- It's one line of code vs. so much Python hacks
Answered By - jpic
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.