I am creating a new application using Django, my application is in need of a form to add data on the db. I am using 4 models which are all connected by foreign keys. How can I use modelForm's to enter data to the form and add them directly to db, if it's not possible what should be my approach?
I checked the existing solutions which are for 2 models only, I couldn't find a way to tweak it to my own problem. I tried using inlineformset but as I said couldn't figure out how can I implement it with all my models together.
I also looked through the Django documentation couple times but couldn't find anything this sort (maybe I am missing).
My models are like this:
class Question(models.Model):
year = models.CharField(max_length=5)
source = models.CharField(max_length=140)
problemNumber = models.CharField(max_length=16)
weblink = models.CharField(max_length=200)
date_posted = models.DateTimeField(default=timezone.now)
problemStatement = models.TextField()
author = models.ForeignKey(User, on_delete=models.PROTECT)
class QLevel(models.Model):
qID = models.ForeignKey(Question, on_delete=models.CASCADE)
level = models.CharField(max_length=16)
class QMethod(models.Model):
qID = models.ForeignKey(Question, on_delete=models.CASCADE)
method = models.CharField(max_length=40)
class QSubject(models.Model):
qID = models.ForeignKey(Question, on_delete=models.CASCADE)
subject = models.CharField(max_length=35)
As can be seen 3 of the models are connected to Question model with a foreign key, whilist Question model is connected to User.
I have a simple form that I basically input all the information needed (all the fields given) and want it to feed them directly to the db. If this is possible please guide me, if not please explain why not and what other approaches can I try?
Thank you!