W
W
WhiteJamer2019-07-16 16:01:51
Django
WhiteJamer, 2019-07-16 16:01:51

How to organize forms for Django objects bound via ForeignKey?

In general, you need to make it so that the user can enter "question" and "answer options" on the same page, while there should be no restrictions on answer options, I think that there should be ajax in some place, but unfortunately I'm still on my way his study.
Been struggling with this all night and ended up only redirecting to a page with an additional form for adding answer options, but that's not it!
Model code:

from django.db import models

class Question(models.Model):
    question_text = models.CharField(max_length=200)
    pub_date = models.DateTimeField(auto_now_add=True)
    def __str__(self):
        return self.question_text
class Choice(models.Model):
    choice_text = models.CharField(max_length=200)
    question = models.ForeignKey(Question, on_delete=models.CASCADE)
    votes = models.IntegerField(default=0)
    def __str__(self):
        return self.choice_text

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Astrohas, 2019-07-16
@Whitejamer

Inline Formsets
https://docs.djangoproject.com/en/2.2/topics/forms...
well, the "Add more" button can be done something like this https://stackoverflow.com/a/8097617/11535126

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question