[Fixed]-Django Tutorial: .choice_set.all not being called

-1đź‘Ť

âś…

As a work around, you can add a function to your question model that grabs all relative information and generates a dictionary. Pass the dictionary through and you can access the information through the template.

This is a small example in my project

models.py under the student class

def get_homework(self):
    '''get all homeworks of classes related to this student'''
    dict = {}
    dict['notification'] = False
    dict['content'] = {}
    for my_class in self.related_class.all():
        homework_list = []
        for class_homework in my_class.related_homework.filter(due_date__gte=date.today()):
            if class_homework.due_date == date.today():
                dict['notification'] = True
            homework = {}
            homework['name_chinese'] = class_homework.name_chinese
            homework['assigned_by'] = class_homework.assigned_by
            homework['homework_content'] = class_homework.homework_content
            homework['assign_date'] = class_homework.assign_date
            homework['due_date'] = class_homework.due_date
            homework['submission'] = class_homework.submission
            homework_list.append(homework)
        dict['content'][my_class.name_chinese] = homework_list
    return dict

views.py

dict = student.get_homework()
return render(request,'ParentHomework.html',dict)
👤Bobby

1đź‘Ť

Register “Choice” in “polls/admin.py”

from django.contrib import admin
from .models import Question
from .models import Choice

admin.site.register(Question)
admin.site.register(Choice)

After that add some choices in your admin panel and you’re good to go.

👤Yooy Altroo

1đź‘Ť

Here we have created two models i.e. “Question” & “Choice”.

By following the tutorial we have added data in Question model by using our Admin privilege.
But we have not added anything in “Choice” table that’s why “question.choice_set.all” is returning nothing(null) and because of that the “{% for choice in question.choice_set.all %}”
is not executing.

I have just added a line of code for demo in details.html

{{ question.question_text }}

{{ question.choice_set.all }}

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{%endif %}

enter image description here

Here as we can see QuerySet is empty.

I have manually entered data in “Choice” table after that when I am executing my code I am getting radio button and label in output which was expected and it shows that for loop is working.
enter image description here

While entering data in Choice table just keep in mind that question_id is foreign key in choice table. So its value should match any one value of id from question table.

There might be other better solution, but this is what I have figured out. I am also a newbie trying to learn django.

Happy Coding!!

👤yugal sinha

0đź‘Ť

choice_set.all

relatedclasslowercasename_set.all

Reference:

https://www.reddit.com/r/django/comments/22poxo/basic_django_tutorial_question_about_pchoice/

👤Anzárian

Leave a comment