如何链接其他Django模型的用户?

时间:2018-01-17 02:52:06

标签: python django django-models

我正在尝试将两个应用程序与不同的数据库模型链接起来。我已经路由但当我想将users.user模型与myapp.UserView链接时给出了这个错误:

Name                          samAccountName                Enabled                       ObjectClass                  
----                          --------------                -------                       -----------                  
Iliyas Ismail                 ilismail                                                    user                         
Ramesh Sengodan               rasengodan                                                  user 

Myapp.model.py

(fields.E300) Field defines a relation with model 'users.User', which is either not
installed, or is abstract.
(fields.E307) The field with myapp.UserView.user was declared with a lazy reference to 
'users.user', but app 'users' doesn't provide model 'user'.

users.model.py

class UserView(models.Model):
    user = models.OneToOneField('users.User', on_delete=models.CASCADE)
    view_id = models.CharField(max_length=45)
    view_name = models.CharField(max_length=255)

有任何线索吗?

1 个答案:

答案 0 :(得分:0)

django用户模型独立于您的应用。因此,您只需要像这样导入它:

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

class UserView(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    view_id = models.CharField(max_length=45)
    view_name = models.CharField(max_length=255)