Answer the question
In order to leave comments, you need to log in
How to combine multiple models on one page in Django admin?
Guys, hello.
My task: The page consists of many blocks that need to be edited on one page in the Django admin.
Problem: the page also contains a questionnaire: 3 questions, each question has 3 possible answers. I want to make it so that there is a question below it, there are three cells for answers, then another question (3 answers), and so on. And this doesn't work for me.
Model code
class Step(models.Model): #Общая модель для контента на странице
title = models.CharField(max_length=200)
description = models.CharField(max_length=200)
annotation = models.TextField()
main_text = models.TextField()
main_photo = models.ImageField(upload_to='static/bakot/imagination/', height_field=None, width_field=None, max_length=100)
navigation_text = models.CharField(max_length=200)
true_question = models.TextField()
true_answer = models.TextField()
def __str__(self):
return self.title
class Question(models.Model): # Модель для вопроса
step = models.ForeignKey(Step, on_delete=models.CASCADE)
question_text = models.CharField(max_length=200)
class Answer(models.Model): #Модель для ответов.
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
from bakot.models import Step, Question, Answer
class AnswerType(admin.TabularInline):
model = Answer
extra = 3
class QuestionType(admin.TabularInline):
model = Question
extra = 3
inlines = [
AnswerType,
]
class QuestionAdmin(admin.ModelAdmin):
inlines = [
QuestionType,
]
admin.site.register(Step, QuestionAdmin)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question