顯然它是一個循環導入,但是我需要知道如何分解它並仍然保留我的功能。無法從App1.models導入模型,而App.2正在導入App1的模型
我的應用程序Profile
有一個叫Notification
模型,這個模型Notification
具有與我的Track
模型,它是我在其他應用程序Submission
一個ForeignKey
。 Submission
也有一個名爲Comment
的模型,這有一個保存方法,它也應該創建一個Notification
。
內Profile.models.py:
from django.db import models
from django.contrib.auth.models import User
from Submission.storage import OverwriteStorage
from Submission.models import Track
from django.core.cache import cache
from project import settings
from datetime import datetime
Profile.models.py:
...
class Notification(models.Model):
user = models.ForeignKey(User, related_name='reciever')
sender = models.ForeignKey(User, related_name='sender')
track = models.ForeignKey(Track, related_name='track')
content_type = models.CharField(max_length=30)
content = models.CharField(max_length=200)
created = models.DateTimeField(auto_now=False,auto_now_add=True)
class Meta:
ordering = ('-created',)
def save(self, *args, **kwargs):
super(Notification, self).save(*args, **kwargs) # Call the "real" save() method
UserProfile.objects.filter(id=self.user.userprofile.id).update(unread_notifications=True)
這將導入 「軌道」,你可以看到,現在裏面Submission.models.py:
from django.db import models
from django.core.exceptions import ValidationError
from django.core.files.images import get_image_dimensions
from django.contrib.auth.models import User
from Submission.storage import OverwriteStorage
from Profile.models import Notification
class Track(models.Model):
...
class Comment(models.Model):
user = models.ForeignKey(User, unique=False)
track = models.ForeignKey(Track, unique=False, related_name = 'comments')
content = models.TextField(max_length=450)
created = models.DateTimeField(auto_now=False,auto_now_add=True)
edited = models.BooleanField(default=False)
replies = models.ManyToManyField('Comment', blank=True)
score = models.BigIntegerField(default=0)
class Meta:
ordering = ('-created',)
def save(self, *args, **kwargs):
super(Comment, self).save(*args, **kwargs)
notification = Notification(user=self.track.user, sender=self.user, track=self.track, content_type='comment', content=self.content)
所以在這裏你可以看到我創建評論時創建通知。不過,我不能這樣做,因爲我必須從已從導入中導入跟蹤的配置文件導入通知。我可以在我的視圖中創建通知,但我覺得這不是Django的方式。
您尚未解釋爲什麼需要將跟蹤導入通知。 –
@DanielRoseman呃,對不起。通知需要跟蹤關聯。因此,用戶可以在用戶界面中單擊該通知,並將它們帶到被評論的軌道。編輯:我已經添加了缺少的代碼。 –