title: How to have pre/post populated id field in django form submission
body: <p>What I am trying to achieve is to have my &quot;student_id&quot; auto-generated field to be part of the form submission in the sense that the &quot;student_id&quot; field does not have to be manually input in form submission:</p>
  <p>The &quot;student_id&quot; field needs to be either pre-generated upon displaying the form or to be generated upon form submission.</p>
  <p>I have tried and currently facing error when submitting form as bellow:</p>
  <pre><code>KeyError at 'student_id'
  
  Exception Value: 'student_id'
  </code></pre>
  <p>Removing the &quot;student_id = form.cleaned_data['student_id']&quot; syntax in views.py does not help either.</p>
  <p>I have the following model, which generates an auto &quot;student_id&quot; field</p>
  <pre><code>class Student(models.Model):
  student_id = models.AutoField(primary_key=True,unique=True, max_length=10, blank=False)
  name = models.CharField(max_length=200, blank=False)
  first_name = models.CharField(max_length=100, blank=True)
  last_name = models.CharField(max_length=100, blank=True)
  
  def __str__(self):
      return self.name
  </code></pre>
  <p>my <code>forms.py</code>:</p>
  <pre><code>    Class StudentForm(forms.ModelForm):
          class Meta:
              model = Student
              fields = [
                  'student_id','name', 'first_name', 'last_name'
              ]
  </code></pre>
  <p>my <code>views.py</code>:</p>
  <pre><code>    def index(request):
          if request.method == 'POST':
              form = StudentForm(request.POST, or None)
              if form.is_valid():
                  student_id = form.cleaned_data['student_id']
                  form.save()
              else:
                  FormError = StudentForm.errors
              
              return redirect(f'/card/{student_id}')
          else:
              form = StudentForm()
      
          template_name = 'index.html'
          context = {
              'form' : form,
          }
          return render(request, template_name, context)
  </code></pre>
  <p>my <code>html</code>:</p>
  <pre><code>&lt;form method=&quot;POST&quot; action=&quot;{% url 'student:index' %}&quot; enctype=&quot;multipart/form-data&quot;&gt;{% csrf_token %}
  
              &lt;div class=&quot;form-group&quot;&gt;
                  {{ form | crispy }}
              &lt;/div&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Envoyer&quot; class=&quot;btn btn-primary btn-lg btn-block&quot;&gt;
      &lt;/form&gt;
  </code></pre>
  <p>Will appreciate any help</p>