django - 用户之间的消息传递应用程序模型

时间:2013-11-29 15:27:55

标签: django django-models

如何在用户之间创建消息应用。创建模型的业务逻辑是什么?我能想到的就是这样:

models.py

class Message(models.Model):
    description = models.TextField()
    date_added = models.DateTimeField(default=datetime.now)
    sender = models.ForeignKey(User)
    recipient = models.ForeignKey(User)

我不太确定这是否可行。如果你能指导我如何开始,将非常感谢。谢谢!

1 个答案:

答案 0 :(得分:5)

常见病例的变体:

from django.utils.translation import ugettext_lazy as _

class Message(models.Model):
    """
    A private message from user to user
    """
    subject = models.CharField(_("Subject"), max_length=120, blank=True)
    body = models.TextField(_("Body"))
    sender = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='sender_messages', verbose_name=_("Sender"), )
    recipient = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='receiver_messages', null=True, blank=True, verbose_name=_("Recipient"))
    parent_msg = models.ForeignKey('self', related_name='next_messages', null=True, blank=True, verbose_name=_("Parent message"))
    sent_at = models.DateTimeField(_("sent at"), null=True, blank=True)
    read_at = models.DateTimeField(_("read at"), null=True, blank=True)
    replied_at = models.DateTimeField(_("replied at"), null=True, blank=True)
    sender_deleted_at = models.DateTimeField(_("Sender deleted at"), null=True, blank=True)
    recipient_deleted_at = models.DateTimeField(_("Recipient deleted at"), null=True, blank=True)
    ip = models.GenericIPAddressField(verbose_name=_('IP'), null=True, blank=True)
    user_agent = models.CharField(verbose_name=_('User Agent'), blank=True, max_length=255)