A
A
alexkomp2019-08-31 10:07:59
Django
alexkomp, 2019-08-31 10:07:59

How to correctly display all contests with the data of these contests in a Dajngo template?

I have a contest model, it has a question model, and in the question model, it has a choice model.
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='questi')
    
    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(nested_admin.NestedStackedInline):
    model = Questions_var
    extra = 1


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


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

I tried to display all objects of the competition questions through a function in the model:
def get_questions(self):
        return self.questions_set.all()

But it doesn't output anything to the template. I thought that I was incorrectly outputting to the template through the constructor and decided to output to the console, but it still didn’t output.
How to correctly display all contests with the data of these contests in a Dajngo template?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alternativshik, 2019-08-31
@alexkomp

read until enlightenment
https://docs.djangoproject.com/en/2.2/topics/db/qu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question