0

I have the following two models in Django. Model Contest has questions and Questions is another django model:

class Contest(models.Model):
    min_questions = models.IntegerField()

class Question(models.Model):
    contest = models.ForeignKey(Contest, on_delete=models.CASCADE, related_name="questions")
    question = ...

Then I add the questions as an inline in the contest admin:

class QuestionInlineForm(forms.ModelForm):
    class Meta:
        model = Question
        fields = \['question'\]

class QuestionInline(NestedStackedInline):
    model = Question
    form = QuestionInlineForm
    extra = 1
    inlines = \[AlternativeInline\] #Igonre this

class CustomContestAdmin(NestedModelAdmin):
    inlines = \[QuestionInline,\]

This let me create the questions inside the contests form, the problem is that I want to restrict the number of questions to create using the field min_questions of the Contest model, so that the form doesn't let me create a contest if the total amount of questions is less than the field min_questions. Is there a way to achieve this?

I'm using the nested_admin library and I tried the method clean but the questions count shows as 0 because the questions have to be added after the instance of contest is created. I think this could be done by using transactions so I can create the Contest, then add the questions and before saving to the database check if the questions count is higher than the min_questions but I can't find how to do this. I also thought of counting the inline questions fields added in the clean method but I didn't have luck with that either.

0 Answers0