我正在加入两个Django应用程序,他们将拥有"用户"共同两个应用程序都在不同的数据库中。
我使用的是路由器,一切正常,但是当我尝试从其他数据库导入用户时出现问题。
我的用户模型如下所示:
class Company(models.Model):
company_name = models.CharField(max_length=250, null=True)
active = models.BooleanField(default=True)
class Meta:
verbose_name = "Company"
verbose_name_plural = "Companies"
def __str__(self):
return self.company_name
class CompanyUser(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
company = models.ForeignKey(Company)
token = models.TextField(null=True,blank=True)
class Meta:
verbose_name = "Users - Company"
verbose_name_plural = "Users - Company"
在我看来,我有这个:
from users.models import Company, User
company = request.user.username #This is fine
company = request.user.companyuser.company This gives error
我收到此错误
**User has no companyuser**
答案 0 :(得分:1)
companyuser不是用户属性,请尝试以下操作:
company = Company.objects.filter(companyuser__user=request.user)