Django进口模型.pyFK公司

2024-04-20 12:57:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我在notifications应用程序中有一个模型。在

class Notification(models.Model):
    name = models.CharField(max_length = 255, primary_key = True)
    description = models.CharField(max_length = 255)

以及Server应用程序中的另一个模型。在

^{pr2}$

即使我从通知应用程序导入模型,我仍然收到一个错误消息

Unhandled exception in thread started by <function wrapper at 0x7fb523bad0c8>
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/django/utils/autoreload.py", line 93, in wrapper
    fn(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/django/core/management/commands/runserver.py", line 101, in inner_run
    self.validate(display_num_errors=True)
  File "/usr/lib/python2.7/dist-packages/django/core/management/base.py", line 314, in validate
    raise CommandError("One or more models did not validate:\n%s" % error_text)
django.core.management.base.CommandError: One or more models did not validate:
servers.servernotificationmapping: 'notification' has a relation with model Notification, which has either not been installed or is abstract.

Tags: djangoinpycore模型应用程序modelslib
1条回答
网友
1楼 · 发布于 2024-04-20 12:57:08

尝试(使用实际类名而不是字符串):

class ServerNotificationMapping(models.Model):
    server = models.ForeignKey('Server', related_name = 'servers')
    notification = models.ForeignKey(Notification,related_name = 'notifications')
    class Meta:
        unique_together = (("server", "notification"),)

相关问题 更多 >