简体   繁体   English

在Django中访问与模型具有OneToOne关系的对象

[英]Accessing objects with OneToOne relationship to a model in Django

I have the following models, which are dependent on django.contrib.auth.models.User . 我有以下模型,这些模型取决于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')
    ...

How can I get a QuerySet of User objects who are related to Teacher ? 如何获得与Teacher相关的User对象的QuerySet

User.objects.exclude(teacher=None)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django:通过相关的 OneToOne model 使用 through 参数序列化 ManyToMany 关系 - Django: Serialize a ManyToMany relationship with a through argument via a related OneToOne model 使用Auth,用户模型,视图,序列化器的Django API OneToOne关系 - Django API OneToOne relationship using Auth, User model, Views, Serializers Django访问ForeignKey模型对象 - Django accessing ForeignKey model objects 在Django中访问具有外键关系的对象? - Accessing objects with a foreign key relationship in Django? 我在Django应用程序中的同一个类的两个对象之间有一个OneToOne关系。 是否有可能强制实现这种关系的独特性? - I have a OneToOne relationship between two objects of the same class in a Django app. Is it possible to enforce the uniqueness of this relationship? 在Django中,对于同一类的两个对象之间的OneToOne关系,是否可以防止某个对象在数据库级别引用其自身? - In Django, for a OneToOne relationship between two objects of the same class, can an object be prevented from referring to itself at the db level? 在Django模型中提取OneToOne字段 - Extract OneToOne Field in django model Django 与用户具有一对一关系的客户 - 创建用户时如何为该 model 创建字段? - Django Customer with onetoone relationship with User - how to create the fields for that model when creating User? 如何在Django Admin中格式化OneToOne关系? - How to format OneToOne Relationship in Django Admin? 一对一关系和 django-autocomplete-light - OneToOne relationship and django-autocomplete-light
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM