how to get data from multiple tables in django

Solutions on MaxInterview for how to get data from multiple tables in django by the best coders in the world

showing results for - "how to get data from multiple tables in django"
Abagail
10 Nov 2020
1class Student(models.Model):
2    sid = models.BigIntegerField(primary_key=True)
3    nm = models.CharField(max_length=20, blank=True, null=True)
4    subject = models.ForeignKey("Subject", related_name="student", blank=True, null=True)
5
6class Subject(models.Model):
7    sub = models.CharField(max_length=30, blank=True, null=True)
8
Erika
09 Jan 2017
1from App.models import Student, Subject
2def myFunc():
3    students = Student.objects.all()
4    for student in students:
5        subj = student.subject.sub
6
7def myFunc2():
8    subjects = Subject.objects.all()
9    for subject in subjects:
10        student_name = subject.student.nm
11