Same form saving to related models

33 views
Skip to first unread message

Elorm Koku

unread,
Jun 9, 2017, 12:44:07 AM6/9/17
to django...@googlegroups.com
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

Elorm Koku

unread,
Jun 9, 2017, 6:20:54 PM6/9/17
to django...@googlegroups.com
No luck for me?

yingi keme

unread,
Jun 10, 2017, 8:21:02 AM6/10/17
to django...@googlegroups.com
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
--
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...@googlegroups.com.
To post to this group, send email to django...@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.

Elorm Koku

unread,
Jun 10, 2017, 10:02:12 AM6/10/17
to django...@googlegroups.com
Thanks yingi, u saved a life :)

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Jun 10, 2017, 10:40:30 AM6/10/17
to django...@googlegroups.com

On Saturday 10 June 2017 13:20:11 yingi keme wrote:

 

> 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?

 

And typically, students can have more then one parent. So either rename your Parent model as Parents (plular) to indicate that you use one object to describe two natural persons (and indeed, switch the foreign key) - or - use a ManyToMany as both sides of the relation can be linked to more then one of the other.

 

In practice having one object for two Parents isn't ideal, as half of the parents end up in the divorce, so contact details and so on, become a mess again.

 

ManyToManyField is the best fit here.

 

--

Melvyn Sopacua

Elorm Koku

unread,
Jun 10, 2017, 11:43:10 AM6/10/17
to django...@googlegroups.com
Melvin thanks, actually my models are simplified...the switch of fk was a way of trying to use it at d front end so that I could provide an id to link em up...unfortunately I can't go around it since an instance of the model is not easily accessible cos its a new object not saved yet...I thought of using a widget form to add up a parent later but I think yingis suggestions are smoother

--
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...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

yingi keme

unread,
Jun 10, 2017, 11:44:20 AM6/10/17
to django...@googlegroups.com
Yea exactly. A many to many relationship is the best approach on this. 

Elorm you should try many to many relationship if you are not completely satisfied.

Thanks Melvyn

Yingi Kem
--
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...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Melvyn Sopacua

unread,
Jun 10, 2017, 12:07:11 PM6/10/17
to django...@googlegroups.com

On Saturday 10 June 2017 15:42:50 Elorm Koku wrote:

> Melvin thanks, actually my models are simplified...the switch of fk

> was a way of trying to use it at d front end so that I could provide

> an id to link em up...unfortunately I can't go around it since an

> instance of the model is not easily accessible cos its a new object

> not saved yet...

 

This is all handled for a ManyToManyField in the admin and can be customized using InlineModelAdmin.

 

It can be done in the front-end using InlineFormSet.

 

 

> I thought of using a widget form to add up a parent

> later but I think yingis suggestions are smoother

>

> On Jun 10, 2017 2:39 PM, "Melvyn Sopacua" <m.r.s...@gmail.com> wrote:

> > On Saturday 10 June 2017 13:20:11 yingi keme wrote:

> > > 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?

> >

> > And typically, students can have more then one parent. So either

> > rename your Parent model as Parents (plular) to indicate that you

> > use one object to describe two natural persons (and indeed, switch

> > the foreign key) - or - use a ManyToMany as both sides of the

> > relation can be linked to more then one of the other.

> >

> >

> >

> > In practice having one object for two Parents isn't ideal, as half

> > of the parents end up in the divorce, so contact details and so on,

> > become a mess again.

> >

> >

> >

> > ManyToManyField is the best fit here.

> >

> >

> >

> > --

> >

> > Melvyn Sopacua

> >

> > --

> > 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...@googlegroups.com.

> > To post to this group, send email to django...@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/1653535.zO1WQPNAma%40devstation

> > <https://groups.google.com/d/msgid/django-users/1653535.zO1WQPNAma%4

> > 0devstation?utm_medium=email&utm_source=footer> .

> > For more options, visit https://groups.google.com/d/optout.

 

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages