title: Django : Extract data from table to initially populate a form
body: <p>I'm looking to initially populate my Django form with data based from another app. I followed this SO example in order to write my function : </p>
  
  <p><a href="https://stackoverflow.com/questions/3833403/initial-populating-on-django-forms">Initial populating on Django Forms</a></p>
  
  <p>However, I didn't succed to get what I want. This is what my function is supposed to do :</p>
  
  <p>I have a model which is named : <code>Person</code> with lots of informations (firstname, ...) and a very useful <code>unique number</code> in order to distinguished each person.</p>
  
  <p>Then, I have another model which is named <code>BirthCertificate</code> with a form in order to create a Birth Act. In my form view, user gives this <code>unique number</code> and Django is able to search the good person (not hard) et initially populate my form (which have some common fields between Person.model and BirthCertificate.model).</p>
  
  <p>I have 2 steps :</p>
  
  <ul>
  <li>First : User writes person's unique number</li>
  <li>Second : User writes parents' unique number</li>
  </ul>
  
  <p>I submit this informations and the first step should initially populate common fields. Second step works pretty well.</p>
  
  <p>This is my function, but somethings still isn't working :</p>
  
  <pre><code>def BirthCertificate_Form_unique_number(request) :
  
      #User fill some fields
      query_social_number = request.GET.get('social_number')
      query_social_number_father = request.GET.get('social_number_father')
      query_social_number_mother = request.GET.get('social_number_mother')
  
      if query_social_number :
          query_social_number_list = Person.objects.filter(social_number=query_social_number)
          if query_social_number_list:
              sc_obj = query_social_number_list[0]
              #For example : sc_obj.lastname will extract lastname
              # etc ...
  
      else :
          query_social_number_list = Person.objects.none()
  
      initial_data = {
           'lastname': sc_obj.lastname, 
           'firstname': sc_obj.firstname, 
           'birthday': sc_obj.birthday, 
           'sex' : sc_obj.sex, 
           'birthcity': sc_obj.birthcity
           }
  
      form = BirthCertificateForm(request.POST or None, initial=initial_data)
  
      if form.is_valid() :   # Check data
          post = form.save()
          return HttpResponseRedirect(reverse('BC_treated', kwargs={'id': post.id}))
  
      else:
  
          form = BirthCertificateForm()
  
          parent1 = Person.objects.filter(social_number=query_social_number_father)
          parent2 = Person.objects.filter(social_number=query_social_number_mother)
  
          form = BirthCertificateForm(request.POST or None)
          form.fields['fk_parent1'].queryset = parent1
          form.fields['fk_parent2'].queryset = parent2
  
      context = {
          "form" : form,
          "person" : person,
      }
  
      return render(request, 'BC_form2.html', context)
  </code></pre>
  
  <p>How can I re organise my part about <code>query_social_number</code> ?</p>
  
  <p>Thank you</p>