Hide field from django model model

The django documentation suggests that I can identify hidden / visible fields from my template. I have two models: AddressInfo and PharmacyInfo. I need to enter data for both tables from one 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 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 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 field for the address_id field, but the label is still displayed. I want to hide the entire div, but 'form.visible_fields' does not exclude it from the output. It drives me crazy. Can someone tell me how to mark the address_id field in a way visible to the template.

+4
source share
2 answers

OK, it looks like I was editing the wrong code in my template. I added

 {% for field in form.visible_fields %} 

to the part of the template that I commented on (I find it difficult to see comment lines on templates). Everything works as expected, using the code below in my template:

 {% 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 %} 

Apologizing to the person who spent the time on this (I would have published earlier, but was new to Stackoverflow, I was not allowed to answer my question within 8 hours after publication).

0
source

If you do not intend to edit the form in the address_id field at all, you should use the exclude meta field as described in Using a subset of the fields in the form .

The result should look like this:

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

Please note that this will prevent the address_id field from being configured through the form (mark the note in the example in the docs).

+19
source

All Articles