Hide a field from a django modelform

Aidan Ewen picture Aidan Ewen · Jun 13, 2011 · Viewed 15.5k times · Source

The django documentation suggests I can identify hidden / visible fields from within my template. I have two models, AddressInfo and PharmacyInfo. I need to enter data for both tables from a single page. I want to hide the address_id field (from the PharmacyInfo model). I currently have:

    {% for form in formset %}
    {% if form.non_field_errors %}
        <div class="form_errors">
            {{ form.non_field_errors }}
        </div>
    {% endif %}

    {% for field in form.visible_fields%}
        <div class="field_content">                  
                <label>{{field.label_tag }}</label>
        <div class="field">

            {% if field.help_text %}
            <div class = "help_text">
                {{ field.help_text }}
            </div>
            {% endif %}               
                {{ field }}
                {{ field.errors }}
        </div> 
        </div>
    {% endfor %}
{% endfor %}



class PharmForm(ModelForm):
    class Meta:
        model = PharmInfo
        widgets = {
            'address_id': forms.HiddenInput()
        }



class AddressForm(ModelForm):
    class Meta:
        model = AddressInfo



class AddressInfo(models.Model):
    address_id = models.AutoField(primary_key=True)

    address_1 = models.CharField("Address Line 1", max_length = 64)
    address_2 = models.CharField("Address Line 2", max_length = 64, blank=True, null=True)
    address_3 = models.CharField("Address Line 3", max_length = 64, blank=True, null=True)
    address_4 = models.CharField("Address Line 4", max_length = 64, blank=True, null=True)
    town_city = models.CharField("Town or City", max_length = 32)
    post_code = models.CharField("Post Code", max_length = 8)
    phone = models.CharField("Phone Number - numbers 0-9 only", 
                        max_length = 16) #this must be numeric 0-9 only for auto-dial functionality.
    email = models.CharField("email address", max_length = 64) #must be valid email



class PharmInfo(models.Model):
    pharm_id = models.AutoField(primary_key=True)
    pharm_name = models.CharField("Pharamcy Name", max_length = 64)
    address_id = models.ForeignKey(AddressInfo, db_column="address_id")



def pharmView (request, id=None):
    pharmForm = PharmForm()
    addForm = AddressForm()
    if request.method == 'POST': #this is a form submission
        if id == None: #it's a new record
            pharmForm = PharmForm(request.POST)
            addForm = AddressForm(request.POST)
            if addForm.is_valid(): #add the address_id into the pharmForm.
                request.POST = request.POST.copy()
                request.POST['address_id'] = addForm.save().pk
                pharmForm = PharmForm(request.POST)
                if pharmForm.is_valid(): 
                    pharm = pharmForm.save()
                    return render_to_response('thanks.html', 
                        {'form1': pharmForm,
                         'form2': addForm},
                        context_instance=RequestContext(request),
                    )

        else: #it's an existing record
            pharm = PharmInfo.objects.get(pk=id)
            address = pharm.address_id
            pharmForm = PharmForm(request.POST, instance = pharm)
            addForm = AddressForm(request.POST, instance = address)
            if pharmForm.is_valid() and addForm.is_valid():
                pharmForm.save()
                addForm.save()
                return render_to_response('updateThanks.html',
                    {'form1': pharmForm,
                     'form2': addForm},
                    context_instance=RequestContext(request),
                )
    else: 
        if id != None: #form bound to a pharmacy record
            pharm = PharmInfo.objects.get(pk=id)
            address = pharm.address_id
            pharmForm = PharmForm(instance = pharm)
            addForm = AddressForm(instance = address)
            return render_to_response('institutions/pharm.html', 
                {'form1': pharmForm,
                 'form2': addForm},
                context_instance=RequestContext(request),
                )            

    return render_to_response('institutions/pharm.html', 
            {'form1': pharmForm,
             'form2': addForm},
            context_instance = RequestContext(request),
        )

This code hides the input box for the address_id field but the label is still shown. I want to hide the whole div but 'form.visible_fields' isn't excluding it from the output. This is driving me nuts. Can anyone tell me how to mark the address_id field in a way visible to the template.

Answer

Andr&#233; Caron picture André Caron · Jun 13, 2011

If you don't intend on the address_id field to be edited at all in the form, you should use the exclude meta field as documented in Using a subset of fields in the form.

The result should look like:

class PharmForm(ModelForm):
    class Meta:
        model = PharmInfo
        exclude = ('address_id',)

Note that this will prevent the address_id field from being set through the form (check the note under the example in the docs).