S
S
Sergey Nizhny Novgorod2015-12-24 05:55:42
Django
Sergey Nizhny Novgorod, 2015-12-24 05:55:42

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)

admin code:
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)

As a result, it turns out download?id=QXfm0t92JOeHOgq48lE7XDJStWUe
Ie . Answers are not included. That is the problem.
Ps I myself am not very familiar with Django, if we say we need to put this whole thing into a separate application, or do something differently, I will be glad to hear.
Pss I read the documentation that there was one inheritance, about double inheritance, I did not find anything.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2015-12-24
@Terras

If you like doing it wrong then stackoverflow.com/questions/14308050/django-admin-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question