2011-06-12 9 views
1

이메일 스레드를 저장하는 django 앱이 있습니다. mbox의 원본 이메일을 구문 분석하여 데이터베이스에 삽입 할 때 이메일 헤더 매개 변수 'message-id'와 'in-reply-to'를 포함합니다. message-id는 메시지를 식별하는 고유 한 문자열이고 in-reply-to는 주어진 메시지가 응답하는 메시지를 식별합니다.메일 헤더 정보를 사용하여 django에서 이메일 스레드 정렬

class Message(models.Model): 
    subject = models.CharField(max_length=300, blank=True, null=True) 
    mesg_id = models.CharField(max_length=150, blank=True, null=True) 
    in_reply_to = models.CharField(max_length=150, blank=True, null=True) 
    orig_body = models.TextField(blank=True, null=True) 

목표는 Gmail이 유사한 스레드 형식의 이메일 대화를 보여줄 수있을 것입니다 : 여기

내 모델의 메시지 부분입니다. 나는 메일 헤더에서 메일 ID (mesg_id in model)와 in-reply-to (in_reply_to in model)를 사용하여 메일을 추적하고 스레딩을 할 계획이었습니다.

SO와 google을 검토 한 후 장고 트리 비아 또는 django-mptt와 같은 라이브러리를 사용해야합니다. 이 두 가지 솔루션 중 하나의 설명서를 검토 할 때 대부분의 모델에서 외래 키 관계를 사용하고있는 것처럼 보입니다.

위 예제 모델에서 django-treebeard 또는 django-mptt를 구현하려면 어떻게해야합니까? mesg_id 및 in_reply_to 필드를 사용하여이 작업을 수행 할 수 있습니까? 나는이 구현 된 경우 다음과 같이

답변

0

, 나는 그것을 시도 할 수 있습니다 - 장고 - mptt를 사용 : 나는 외래 키에 REPLY_TO를 설정 한

from mptt.models import MPTTModel, TreeForeignKey 

class Message(MPTTModel): 
    subject = models.CharField(max_length=300, blank=True) 
    msg_id = models.CharField(max_length=150, blank=True) # unique=True) <- if msg_id will definitely be unique 
    reply_to = TreeForeignKey('self', null=True, blank=True, related_name='replies') 
    orig_body = models.TextField(blank=True) 

    class MPTTMeta: 
     parent_attr = 'reply_to' 

참고. 즉, 메시지 인스턴스가 msg 인 경우 응답 인 Message 인스턴스에 액세스하려면 msg.reply_to을, 메시지에 대한 모든 응답을 수신하려면 msg.replies.all()을 간단히 수행 할 수 있습니다.

이론적으로 msg_id를 기본 키 필드로 사용할 수 있습니다. 개인적으로 데이터를 기본 키와 분리하여 보관하는 것을 선호하지만 내 방식이 더 좋다고 생각하는 특별한 이유를 알지 못합니다.

+1

origianl 게시물을 받기 전에 답장을받는 경우도 있고, orig_in_reply_to를 @ajt로 CharField로 유지하는 것이 Message를 사용하여 도착한 순서대로 메시지를 일치시키는 데 좋습니다. objects.filter (orig_in_reply_to = new_message.msg_id)'입니다. – Udi