0

models.py

class Topic(Model):
  pass

class Question(Model):
  topic = models.Foreignkey(Topic)

class Syllabus(Model):
  topic = models.Foreignkey(Topic)

#The Bridge Model

class Test_Syllabus(Model):
  test = models.Foreignkey(Test)
  syllabus = models.Foreignkey(Syllabus)

class Test (Model):
  pass

I have a test object as follows:

test = Test.objects.get(id=pk)

Using the test object, I want to get the related queryset of questions. So, I tried the following code:

questions = test.test_syllabus_set.all()  #don't know how to go further from here

I think it has something to do with select_related and prefetch_related. Please help me out with a perfect query for this use case.

  • Your model schema looks incorrect to me. Isn't `topic` supposed to be a _grouping_ criterion for questions? If so, then Question should have FK to Topic. Also I'd suggest to decide to whom "bridge" records belong: to syllabus or to test - and to make'em an M2M attribute of that model. Also I don't understand the relation between Syllabus and Topic, and why there is no direct relation between Test and Question. – Ivan Starostin Apr 14 '20 at 10:56
  • Sorry, Question should have FK to Topic (Edited). Regarding Syllabus-Topic relationship, Syllabus has another FK to Exam Model (not shown), point is Syllabus model will show the topics for different exams. And I do have the Test-Question Model having FKs of both Test and Question Model. My intention is to display all questions related to Test-Syllabus Model, so that i can choose certain questions from the result and add it to my Test-Question Model. – user3425929 Apr 14 '20 at 15:45
  • Sorry, but shown schema still lacks sense to me. – Ivan Starostin Apr 14 '20 at 16:26

0 Answers0