0
我在notifications
應用中有一個模型。Django import models.py FK錯誤
class Notification(models.Model):
name = models.CharField(max_length = 255, primary_key = True)
description = models.CharField(max_length = 255)
而在Server
的另一個模型應用程序。
from modules.notifications.models import * # importing notification's model
class Server(models.Model):
url = models.CharField(max_length = 255, unique = True)
@staticmethod
def get_as_dict(server):
server_notification_mapping_obj = ServerNotificationMapping.objects.filter(server = server)
notifications_list = [obj.notification.name for obj in server_notification_mapping_obj]
return {
'id':server.id,
'url':server.url,
'notifications':notifications_list
}
class ServerNotificationMapping(models.Model):
server = models.ForeignKey('Server', related_name = 'servers')
notification = models.ForeignKey('Notification',related_name = 'notifications')
class Meta:
unique_together = (("server", "notification"),)
即使我從通知應用程序導入模型,我仍然得到一個錯誤說
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.
謝謝......它的工作....但1個問題。它會影響關係嗎?另外,我還有其他的FK關係,我在''''裏面提供了類名,所以我應該刪除它們嗎?最佳做法是什麼? – PythonEnthusiast 2014-10-08 05:09:34
此外,它是否建議有不同的應用程序具體models.py文件或所有模型應該合併在一個應用程序。在不同應用程序中使用不同模型的缺點是我必須以不同方式「架構移植」和「移植」不同的應用程序。那麼,推薦哪一個? – PythonEnthusiast 2014-10-08 05:24:25
約定是使用實際的類而不是類名字符串。所以是的,最好爲其他FK關係做同樣的事情。它不影響關係,但它可能與django如何回顧課程有關。 – 2014-10-08 21:07:55