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
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+unsubscribe@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.
No comments:
Post a Comment