[Fixed]-Django One-To-One relationships on forms

1πŸ‘

βœ…

If using modelForms you well need two distinct forms – one for the User model and one for the Profile model. You then display both within the same <form> tag in your template, and validate / save both in your view.

The other solution is to define a plain (non β€˜model’) form with fields for both models, and write the save method by yourself.

0πŸ‘

Assuming that form is an User object which you’re using in the template, you should be able to access the dob field using form.profile.dob.

πŸ‘€Anomitra

0πŸ‘

Many thanks to Bruno who gave the correct answer above. It led me to the following link:

https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html#onetoone

I hope it helps someone.

πŸ‘€Alan Tingey

Leave a comment