django - 在 djago 中,如果您扩展用户模型并使用配置文件模型
问题描述
如果我们想要来自配置文件模型的移动属性,我们如何使用下面的代码?
user.profile.mobile
但是配置文件类有用户引用,这意味着可以像这样访问用户名
profile.user.username
解决方案
定义一个related_name
调用反向访问器:
class Profile(models.Model):
user_name = models.OneToOneField(to= User,on_delete=CASCADE,related_name='profile')
mobile = models.PositiveIntegerField(null = True,blank = True)
打印(request.user.profile.mobile)
推荐阅读
- android - 对齐文本(也就是说,使其适合文本视图并覆盖所有空间)
- python-3.x - 有没有一种保存方法来检索列表的索引而无需尝试捕获?
- firebase - 无法识别的请求类型:Request{com.google.android.gms.tasks.zzu@5843814
- protege - Protege:从属性中创建新类
- redirect - Nginx 301 将 http(裸和 www)重定向到 https www,加上通配符子域到 https
- android - 崩溃 ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1)
- python - Tkinter 中的复选框
- python - Sklearn:从 timeSeriesSplit 获取最后一次拆分
- solr - Apache SOLR - 建议者在建议中省略连字符和方括号
- javascript - Node-hid 模块没有在 webworker 上自行注册