可以在Django中创建类和抽象类之间的关系吗?

时间:2019-06-20 14:38:43

标签: django-models abstract

我尝试创建一个类“ User”,它是“ SpecialUser”和“ SpecialUser2”类的基类。但也想发送通知到SpecialUsers,所以我像在UML中创建了一个类通知。但是每次我使用命令“ makemigrations”时,都会引发此错误:

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

我试图重新格式化它,但是其他方式对我来说没有意义。这是以下代码中的实际UML:https://imgur.com/a/Bhdh8ZE,这是Django模型:

class Notification(models.Model):
    send_date = models.DateField(default=None)
    text = models.CharField(max_length=511, default="")

    user = models.ForeignKey(
        'User',
        on_delete=models.CASCADE,
        default=None
    )

class User(models.Model):
    first_name = models.CharField(max_length=31, default="")
    last_name = models.CharField(max_length=31, default="")

    class Meta:
        abstract = True

class SpecialUser(User):
    pass

class SpecialUser2(User):
    pass

0 个答案:

没有答案