Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#20929 closed Uncategorized (worksforme)

inline_formset - DELETE field is in visible_fields

Reported by: TonyEight Owned by: nobody
Component: Forms Version: 1.5
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

As filled out here, I wonder why DELETE field is in visible_fields yet its widget is input type="hidden". Shouldn't it be in hidden_fields ?

Change History (4)

comment:1 by Tim Graham, 11 years ago

I can't reproduce the DELETE field rendering with input type="hidden". For example, modifying tests.inline_formsets.tests.DeletionTests.test_deletion:

    PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True, fields="__all__")

    for form in formset.forms:
        print [str(field) for field in form.visible_fields()]

    ['<input id="id_poem_set-0-name" maxlength="100" name="poem_set-0-name" type="text" />', 
      '<input id="id_poem_set-0-DELETE" name="poem_set-0-DELETE" type="checkbox" />']

Am I missing something?

comment:2 by TonyEight, 11 years ago

I wonder... It must be me who is missing something...

Here's the template code I use:

   <fieldset class="emails">
        <legend class="text-center">{% trans "E-mail" %}</legend>
        {{ emails.management_form }}        
        <div class="inline-form-emails">
            {{ emails.empty_form.media }}
            {% for hidden in emails.empty_form.hidden_fields %}
            {{ hidden }}
            {% endfor %}
            {% for field in emails.empty_form.visible_fields %}
            <div class="form-group">
                <label for="{{ field.html_name }}" class="col-xs-12 col-sm-5 col-md-3 col-lg-3 control-label">
                    {{ field.label }} {% if field.field.required %}<span style="color: #a60000;">*</span>{% endif %}
                </label>
                <div class="col-xs-12 col-sm-7 col-md-9 col-lg-9">
                    {{ field }}
                    <span class="help-block">{{ field.help_text }}</span>
                </div>
            </div>
            {% endfor %}
        </div>
    </fieldset>

Is something improperly set ? I wonder how my DELETE field get that type="hidden"...

Again, here's the view:

   class ContactCreateView(LoginRequiredMixin, CreateView):
       template_name = u'frontend/contacts/create.html'
       model = Contact
       form_class = ContactCreateForm

       def get_context_data(self, **kwargs):
           context = {
               'emails' : inlineformset_factory(parent_model=Contact, model=ContactEmail, form=ContactEmailCreateForm, extra=0),
           }
           context.update(kwargs)
           return super(ContactCreateView, self).get_context_data(**context)

and my ContactEmailCreateForm:

   class ContactEmailCreateForm(forms.ModelForm):
       # Documentation
       __doc__ = _(u'A custom form for ContactEmail model.')

       # Methods
       def __init__(self, *args, **kwargs):
           super(ContactEmailCreateForm, self).__init__(*args, **kwargs)
           for name, field in self.fields.items():
               if field.widget.attrs.has_key('class'):
                   field.widget.attrs['class'] += ' form-control'
               else:
                   field.widget.attrs.update({'class':'form-control'})

       # Meta-data
       class Meta:
           model = ContactEmail

comment:3 by Tim Graham, 11 years ago

Resolution: worksforme
Status: newclosed

Using these snippets, I can't reproduce the delete field rendering as hidden. If you could try to reproduce the behavior by writing a test for Django, that will help us identify the bug (if one exists). Please reopen this ticket if you can provide that.

comment:4 by TonyEight, 11 years ago

I think I might find where the problem came from. It is not a Django bug nor even an incorrect implementation. As I mentioned it on stack, my intention was to use django-dynamic-formset. This javascript library was responsible for the DELETE field becoming a input type="hidden" instead of the native checkbox.

Anyway, thank you for your tests.

Note: See TracTickets for help on using tickets.
Back to Top