Y
Y
Yatupoi1232021-09-07 21:15:41
Django
Yatupoi123, 2021-09-07 21:15:41

How to add 2 inlines in django admin?

I am new to django and would like to know. Whether it is possible to make somehow that there was a possibility of creation of two inlines? And if not, how can this be done differently, it is desirable that everything can be edited in one table

models.py
from django.db import models

# Create your models here.
class Test(models.Model):
    title = models.CharField(max_length=4096)
    visible = models.BooleanField(default=False)
    max_points = models.IntegerField()
    category = models.CharField(default="default category", max_length=4096)

    def __str__(self):
        return self.title


class Question(models.Model):
    question = models.ForeignKey(Test, on_delete=models.DO_NOTHING)
    question_text = models.TextField()
    question_point = models.IntegerField(default = 1)
    
    def __str__(self):
        return self.question_text

class Choice(models.Model):
    choice = models.ForeignKey(Question, on_delete=models.DO_NOTHING)
    choice_text = models.TextField()
    choice_point = models.IntegerField(default = 0)
        
    def __str__(self):
        return self.choice_text

admin.py
from django.contrib import admin
from .models import Test, Question, Choice

class QuestionInline(admin.StackedInline):
    model = Question
    extra = 1

class TestAdmin(admin.ModelAdmin):
    list_display = (
        'title',
        'visible',
        'max_points',
        'category',
    )
    inlines = [QuestionInline]
    
admin.site.register(Test, TestAdmin)


I want to make it possible to add up to 4 choices in addition to the add another question button in each question field.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikita Mokhovikov, 2021-09-08
@mohovoy

Read documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question