我想联接两个模型并显示联接的所有字段,就像在SQL上一样,例如,我想执行以下请求:
SELECT course_id, challenge_id FROM Challenges,Course WHERE course_id=course;
一门课程有很多挑战。
我的模特:
class Course(models.Model):
class Meta:
unique_together = (('course_id', 'owner',),)
db_table = "course"
REQUIRED_FIELDS = ['owner', 'course_id']
course_id = models.AutoField(primary_key=True)
owner = models.ForeignKey(Users, on_delete=models.CASCADE)
class Challenges(models.Model):
class Meta:
unique_together = (('challenge_id', 'course'),)
db_table = "challenges"
REQUIRED_FIELDS = ['challenge_id', 'course']
challenge_id = models.AutoField(primary_key=True)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
答案 0 :(得分:0)
即使在纯SQL上,您也不需要为此加入任何内容
Challenges.objects.filter(course__isnull=False, ).values('course_id', 'challenge_id', )