Answer the question
In order to leave comments, you need to log in
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
Questions in admin panel
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')
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]
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