Django 外键模糊查询方法

Django里面外键查询不用点查询,而是使用双下划线”__”,如下:

如:查询外键dormitory_id的id包含“D2”的学生

Student.objects.filter(dormitory_id__id__contains = “D2”)

如:查询外键dormitory_id的id包含“D2”的学生(忽略大小写)

Student.objects.filter(dormitory_id__id__icontains = “D2”)

如:查询外键dormitory_id的id开头包含“D2”的学生

Student.objects.filter(dormitory_id__id__startswith = “D2”)

如:查询外键dormitory_id的id结尾包含“D2”的学生

Student.objects.filter(dormitory_id__id__endswith = “D2”)

 

你可能感兴趣的:(python,django,错误解决)