I have this model class:
class question(models.Model):
id = models.CharField(max_length=64, primary_key=True,null=False)
paper_id = models.ForeignKey(paper,null=True)
question_no = models.SmallIntegerField('question_no',max_length=6,null=True)
content = models.TextField('content',null=False)
topic_id = models.ForeignKey(topic,null=True)
subtopic_id = models.ForeignKey(subtopic,null=True)
std_answer = models.CharField(max_length=128,null=True)
marks = models.IntegerField('marks',max_length=2,null=True)
input = models.CharField(max_length=512,null=True,default=None)
type_answer = models.CharField(max_length=512,null=True)
type = models.CharField(max_length=512,null=True,default=None)
def __str__(self):
return str(self.id)
I need to list all its attributes, inclusive of Topic.Title, Subtopic.Title, and Paper.Title which resides in foreign objects Topic, Subtopic, Paper respectively. How may I do so in a single query?
Your models.py must be like this for example:
You don’t have to put id because django will create it for you.
views.py
Your model class is incorrect. You don’t have to put many foreignkey. One foreignkey is OK.