使用OneToOne关系访问Django中的模型

时间:2014-02-24 16:21:12

标签: python django orm

我有以下模型,这些模型取决于django.contrib.auth.models.User

from django.db import models
from django.contrib.auth.models import User

class Teacher(models.Model):
    user = models.OneToOneField(User, related_name='teacher')
    ...

class Student(models.Model):
    user = models.OneToOneField(User, related_name='student')
    ...

如何获得与QuerySet相关的User Teacher个对象?

1 个答案:

答案 0 :(得分:1)

User.objects.exclude(teacher=None)