【发布时间】:2021-01-25 07:52:39
【问题描述】:
我有两个模型,第一个是用户模型,第二个是组织模型。 管理员将负责在组织模型中添加数据。所以我使用了外键。
class Organizaton(models.Model):
name = models.CharField(max_length=255)
status = models.BooleanField(default=True)
created_by = models.ForeignKey(CustomUser,on_delete=models.CASCADE,limit_choices_to= {'is_staff': True})
created_date = models.DateTimeField(auto_now_add=True)
我有一个自定义用户表,我想在其中存储哪个用户属于哪个组织:
class CustomUser(AbstractUser):
organization_name = models.ForeignKey(Organization,on_delete = models.CASCADE)
如果我先声明CustomUser类,那么里面就有错误
显示 class Organization is not Found or declared 的 organization_name 字段中的 CustomUser 类。
如果我在 CustomUser 模型类之前声明组织模型类,那么我会在 created_by 字段中的组织类中收到一个错误,即 CustomUser class not found or declared。
我该如何解决这个问题?
【问题讨论】:
标签: django django-models