forms - Why doesn't my Django template render a ModelForm's id or pk field? -


in django 1.10, i'm prepopulating modelform in template using record database in order edit record in form. however, when want render html form, doesn't show id (or pk) fields. want put id in hidden <input> tag when form submits backend knows record edit when form submitted. web app doesn't have id in url.

here models.py:

from django.db import models  class mymodel(models.model):     name = models.charfield(max_length=50)     phone = models.charfield(max_length=50) 

in database, i've added 1 mymodel record name 'alice' , phone '212-555-5555'. id/primary key 1.

here forms.py:

from django import forms . import models  class testform(forms.modelform):     class meta:         model = models.mymodel         fields = ['id', 'name', 'phone'] 

here views.py:

from django.shortcuts import render . import forms, models  def test_page(request):     m = models.mymodel.objects.get(name='alice')     prepopulatedform = forms.testform(instance=m)     return render(request, 'testproject/test_template.html', {'form': prepopulatedform}) 

here test_template.html:

<h1>test page</h1> {{ form }} 

this renders form on page, prepopulated 'alice' , '212-555-5555':

<h1>test page</h1> <tr><th><label for="id_name">name:</label></th><td><input id="id_name" maxlength="50" name="name" type="text" value="alice" required /></td></tr> <tr><th><label for="id_phone">phone:</label></th><td><input id="id_phone" maxlength="50" name="phone" type="text" value="212-555-5555" required /></td></tr> 

my question is, why doesn't id / primary key column appear in form? can't render when use template either:

<h1>test page</h1> {{ form.id }} {{ form.name }} {{ form.phone }} 

this renders blank id <input> should be:

<h1>test page</h1>  <input id="id_name" maxlength="50" name="name" type="text" value="alice" required /> <input id="id_phone" maxlength="50" name="phone" type="text" value="212-555-5555" required /> 

i can see running python manage.py shell record in fact there:

>>> testproject import models >>> m = models.mymodel.objects.get(name='alice') >>> m.id 1 

in forms.py, setting fields ['id', 'name', 'phone'] doesn't cause errors, nor ['pk', 'name', 'phone']. however, if purposefully add non-existent field name ['not_real_field', 'name', 'phone'], python manage.py runserver produce error:

  file "c:\users\al\venvs\tas_env\lib\site-packages\django\forms\models.py", line 257, in __new__     raise fielderror(message) django.core.exceptions.fielderror: unknown field(s) (not_real_field) specified mymodel 

so know 'id'/'pk' must legitimate field put in modelform.

why doesn't django template render modelform's id or pk field?

eventually, i'd include primary key hidden input in form record in database can edited. note, don't care modeladmin since i'm trying display in template viewable users, not in admin panel. it's fine pk read-only; needs render in template.

how accessing id using {{ form.initial.id }}?

from can tell in looking modelform metaclass, form.initial populated bit differently creation of form properties (e.g. form.name or form.phone). in weeds of django metaclass magic...

to bit more specific. appears when creating form, model_to_dict called pull data out of model instance passed in. isn't until fields_for_model called form instance has model fields accessible class attributes. guess id somehow being excluded fields being considered in fields_for_model. looks non-editable field exception referenced raised here well.


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -