Well. What you are trying to do is a bit awkward, but i will try my best to 
answer.

The variable for foreign key is supposed to be declared under Student model 
because parents can typically have more than one kid in a school isnt it?
You should try and understand relational database.

Now this is how i will write the code

class Parent(models.Model):
   .......

class Student(models.Model):
     parent = models.ForeignKey(Parent)

Now if you want to save with a form. Remember that when you render a form field 
   which contains some kind of relationship, it appears as a drop-down list in 
your webpage. What i suggest is to do the saving in your views.py instead of 
rendering the relational form field. 

So u can do it Like this

def Function(request):
    Obj = Parents.object.get(name='David')
    StudentObj= MyForm({'parent' : Obj})
    StudentObj.save()

Remember the function above is just an assumed prototype of how your function 
should be and not the actual code.

Yingi Kem

> On 9 Jun 2017, at 5:43 AM, Elorm Koku <oluu...@gmail.com> wrote:
> 
> Hi guys,
> I need a means to save these 2 models related by FK but I want to use one 
> form. I searched but I jus can't do it.
> 
> class Student(models.Model):
> .......
> 
> class Parent(models.Model):
> ........
> child = models.ForeignKey(Student)
> 
> 
> All I need is a form I can use to every student and their parent 
> simultaneously.
> Am I doing this wrong? If so kindly correct me.
> Thanks
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/django-users/CAG%2BabP8sVSiKpfkeWtkBrNCoipT1xE%2BPUUkuDNOgaPNd5zeaDw%40mail.gmail.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CDB0032A-2B58-42D1-8CF6-A300299BDF31%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to