根据我读过的 Django 的文档,似乎应用程序文件夹中的 signals.py
是一个很好的起点,但我面临的问题是,当我为 pre_save
创建信号并试图从模型中导入类时,它与我的模型中的 import
冲突。
# models.py
from django.contrib.auth.models import User
from django.db import models
from django.utils.translation import gettext as _
from signals import *
class Comm_Queue(CommunicatorAbstract):
queue_statuses = (
('P', _('Pending')),
('S', _('Sent')),
('E', _('Error')),
('R', _('Rejected')),
)
status = models.CharField(max_length=10, db_index=True, default='P')
is_html = models.BooleanField(default=False)
language = models.CharField(max_length=6, choices=settings.LANGUAGES)
sender_email = models.EmailField()
recipient_email = models.EmailField()
subject = models.CharField(max_length=100)
content = models.TextField()
# signals.py
from django.conf import settings
from django.db.models.signals import pre_save
from django.dispatch import receiver
from models import Comm_Queue
@receiver(pre_save, sender=Comm_Queue)
def get_sender_email_from_settings(sender, **kwargs):
obj=kwargs['instance']
if not obj.sender_email:
obj.sender_email='%s' % settings.ADMINS[0][1]
这段代码不会运行,因为我在 signals.py
中导入了 Comm_Queue
,并且在 models.py
中也导入了信号。
有没有人能给我一些建议,告诉我怎样才能克服这个问题?
问候