简体   繁体   中英

How to fetch data from another app Django

I'm creating a quiz project using Django with MongoEngine. Multiple Choice Questions and quiz are two separated apps. I want to fetch multiple choice in quiz based on m_id (unique number for each multiple choice). I'm a beginner and need a little help. How can i achieve this
MCQS Model

from django_mongoengine import Document, EmbeddedDocument, fields
class multichoice(Document):
    m_id = fields.IntField(min_value=1, verbose_name='MCQ Id')
    m_title = fields.StringField(primary_key=True, max_length=255, verbose_name='Title')
    m_question = fields.StringField(verbose_name='Question')
    m_alternatives = fields.ListField(fields.StringField(),verbose_name='Alternatives')
    
    def __str__(self):
        return self.m_title

Quiz Model

from django_mongoengine import Document, EmbeddedDocument, fields
from mcqs.models import *
class quiz(Document):
    q_id = fields.IntField(min_value=1, verbose_name='Quiz ID')
    q_title = fields.StringField(primary_key=True, max_length=255, verbose_name='Title')
    q_s_description = fields.StringField(max_length=255, verbose_name='Description')
    q_questions = fields.ListField(fields.IntField(), verbose_name='Question Numbers', blank=True)

    def __str__(self):
        return self.q_title

MCQ Views

def view_multichoice(request, m_id):
    get_md = multichoice.objects(m_question_number = m_id)
    return render(request, 'mcqs/mcq.html', {'get_md':get_md})

Quiz Views

def view_quiz(request, q_id):
    get_q = quiz.objects(q_id = q_id)
    return render(request, 'quiz/quiz.html', {'get_q':get_q})

Current Result当前结果 Expected Result预期结果

EDIT 1
Quiz Views

from django.shortcuts import render
from django.http import HttpResponse
from .models import *
from mcqs.models import *
def view_quiz(request, q_id):

    quiz_object = quiz.objects(q_id=q_id)[0]
    multichoice_objects = [multichoice.objects(m_id=id) for id in quiz_object.q_questions]
    get_q = [objects[0].m_question for objects in multichoice_objects if objects]
    return render(request, 'quiz/quiz.html', {'get_q':get_q})

Quiz Template

{{ get_q }}

You are returning the question documents as it is. What you should be doing is, fetch the multichoice documents corresponding to the question ids, and then get the question field from each of those documents.

Change the second line in quiz views to this:

# Get the question document corresponding to given id
# objects method returns a list. Get the first one out of it. 
# Here, I've assumed there's exactly 1 quiz document per ID. Handle the edge cases

quiz_object = quiz.objects(q_id = q_id)[0] 

# Get the multiple choice documents corresponding to the ids in q_questions list
# Remember that objects method returns a list. Hence this is a list of lists
 
multichoice_objects = [multichoice.objects(m_id=id) for id in quiz_object.q_questions]

# Get the actual questions from the multichoice documents

get_q = [objects[0].m_question for objects in multichoice_objects if objects]

Ideally, you should be making multichoice as an EmbeddedDocument and the q_questions field in quiz model as EmbeddedDocumentListField. But the drawback here will be, you can't query EmbeddedDocuments independently. So you wont be able to do multichoice.objects(m_question_number=m_id) .

You can read more about EmbeddedDocuments here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM