fetching data from multiple tables using related name in django

Solutions on MaxInterview for fetching data from multiple tables using related name in django by the best coders in the world

showing results for - "fetching data from multiple tables using related name in django"
Adrianne
04 Jan 2019
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
Jonas
19 Mar 2018
1Order.objects.select_related('account_customer').all() \
2    .values('customer_id', 'other_field_1', ...) \
3    .annotate(total_sales=Sum('total_price')) \
4    .filter(created_at__year=today.year, created_at__month=today.month)
Fergal
28 Jul 2017
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