0

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!

  • 1
    You can create separate forms and add it to same html template. – Bidhan Majhi Jan 27 '19 at 06:51
  • @BidhanMajhi will I be able to update all the models with one 'post'? If you know any source can you guide me? –  Jan 27 '19 at 19:33
  • Do you want all three forms in one post function with different submit button, or you want to update in single submit button? Both cases are possible. I guess you are looking for the second way I have mentioned, https://stackoverflow.com/a/18489593/8966274 this one has a good answer. If you are looking for the first way then let me know, I will add the answer. – Bidhan Majhi Jan 28 '19 at 05:54

1 Answers1

0

After quite some time researching I found the best and easiest way to render multiple foreign key connected forms to be the extension called extra_views. If anyone is looking for a solution, I don't think there is a better one currently.

The solution given here django submit two different forms with one submit button is also working, but if you are using model-based forms like me, with foreign keys, I found this approach to be quite confusing and hard to work with.