A
A
alexkomp2019-08-29 10:39:31
Django
alexkomp, 2019-08-29 10:39:31

How to add an extra object to a django model?

There are three models: Quest - the contest model, Questions - the question model and Questions_var - the answer options for each question.
I made it possible to add additional objects of the Questions model (question model) to the Quest model (competition model).
And I made it possible to add additional Questions_var objects (answer options) to each question (Questions - questions model).
Quest in admin panel
5d677e1895411382476913.png
Questions in admin panel
5d677e790c557349392839.png
models.py

class Quest(models.Model):
    title = models.CharField(max_length = 500)
 
    
    def __str__(self):
        return self.title


class Questions(models.Model):
    title = models.CharField('Название вопроса', max_length=500)
    question_above = models.CharField('Ответ', max_length=1000)
    quest = models.ForeignKey(Quest, on_delete=models.CASCADE, related_name='quest')
    
    def __str__(self):
        return self.title


class Questions_var(models.Model):
    question = models.CharField('Вопрос', max_length=500)
    questions = models.ForeignKey(Questions, on_delete=models.CASCADE, related_name='questions')

admin.py
class QuestionsInline(admin.TabularInline):
    fk_name = 'questions'
    model = Questions_var
    extra = 1


@admin.register(Questions)
class QuestionsAdmin(admin.ModelAdmin):
    inlines = [QuestionsInline]
    
  
class QuestInline(admin.TabularInline):
    fk_name = 'quest'
    model = Questions
    extra = 1


@admin.register(Quest)
class QuestAdmin(admin.ModelAdmin):
    inlines = [QuestInline]

Everything works, but it's inconvenient. How can I make it so that answer options can be entered in Quest models?
So that the answer options are here:
5d67812d0450a516049566.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2019-08-29
@alexkomp

Use nested inlines.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question