import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "modletest.settings")
import django
django.setup()
from apptest import models
# 正向查找
# models.Students.objects.filter(sclass="一班") #这句是错的,ValueError: invalid literal for int() with base 10: '一班'
# obj=models.Students.objects.filter(sclass__cname="一班") #筛选出一班的学生
# obj = obj.first() #得到第一个结果
# obj.sclass #得到sclass的一个对象
# obj.sclass.cname
# 反向查找
# obj = models.Classes.objects.filter(students__sname="张三")
# obj=obj[0]
# obj=obj.students_set.all() # <QuerySet [<Students: 张三>, <Students: 李四>]>
# obj.first().sname #得到张三所在班级的第一个学生的名字
obj = models.Teachers.objects.all().values('teacher_to_class__cls__cname') # 可以通过第三张表进行查询
obj = models.Teachers.objects.filter(tname="张三")
obj = obj[0]
print(obj.teacher_to_class_set.all()) # <QuerySet [<Teacher_to_Class: 1>, <Teacher_to_Class: 5>]>
obj.teacher_to_class_set.all()[0].cls_id.cname
# In [9]: obj.teacher_to_class_set.all()[0].cls
# Out[9]: <Classes: 一班>
#
# In [10]: obj.teacher_to_class_set.all()[0].cls.cname
# Out[10]: '一班'
#
# In [11]: obj.teacher_to_class_set.all()[0].teacher
# Out[11]: <Teachers: 张三>
from django.db import models
# Create your models here.
class Students(models.Model):
sname = models.CharField(max_length=50)
sclass = models.ForeignKey('Classes')
def __str__(self):
return self.sname
class Classes(models.Model):
cname = models.CharField(max_length=50)
def __str__(self):
return self.cname
class Teachers(models.Model):
tname = models.CharField(max_length=50)
def __str__(self):
return self.tname
class Teacher_to_Class(models.Model):
teacher = models.ForeignKey("Teachers")
cls = models.ForeignKey("Classes")
class Meta:
unique_together = (('teacher', 'cls'))
def __str__(self):
return str(self.id)
1 obj=models.Teachers.objects.all()[0]
2 print(obj.cls.all())
3
4 obj = models .Classes .objects.all()[0]
5 print(obj.teachers_set ) #apptest.Teachers.None
6 print(obj.teachers_set.filter(tname="张三"))
7
8
9
10
11
12
13 class Teachers(models.Model):
14 tname = models.CharField(max_length=50)
15 cls = models.ManyToManyField("Classes")
16
17 def __str__(self):
18 return self.tname
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "modletest.settings")
import django
django.setup()
from apptest import models
# 正向查找
# models.Students.objects.filter(sclass="一班") #这句是错的,ValueError: invalid literal for int() with base 10: '一班'
# obj=models.Students.objects.filter(sclass__cname="一班") #筛选出一班的学生
# obj = obj.first() #得到第一个结果
# obj.sclass #得到sclass的一个对象
# obj.sclass.cname
# 反向查找
# obj = models.Classes.objects.filter(students__sname="张三")
# obj=obj[0]
# obj=obj.students_set.all() # <QuerySet [<Students: 张三>, <Students: 李四>]>
# obj.first().sname #得到张三所在班级的第一个学生的名字
obj = models.Teachers.objects.all().values('teacher_to_class__cls__cname') # 可以通过第三张表进行查询
obj = models.Teachers.objects.filter(tname="张三")
obj = obj[0]
print(obj.teacher_to_class_set.all()) # <QuerySet [<Teacher_to_Class: 1>, <Teacher_to_Class: 5>]>
obj.teacher_to_class_set.all()[0].cls_id.cname
# In [9]: obj.teacher_to_class_set.all()[0].cls
# Out[9]: <Classes: 一班>
#
# In [10]: obj.teacher_to_class_set.all()[0].cls.cname
# Out[10]: '一班'
#
# In [11]: obj.teacher_to_class_set.all()[0].teacher
# Out[11]: <Teachers: 张三>