Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6014339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:40:39+00:00 2026-05-23T02:40:39+00:00

I have a django app that stores email threads. When I parse the original

  • 0

I have a django app that stores email threads. When I parse the original emails from an mbox and insert them into the database I include the email header parameters ‘message-id’ and ‘in-reply-to’. The message-id is a unique string that identifies the message, and the in-reply-to identifies a message that a given message is in response to.

Here is the message portion of my model:

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)

The goal is to be able to show email conversations in a threaded format similar to gmail. I was planning on just using the message-id (mesg_id in model) and in-reply-to (in_reply_to in model) from the mail headers to keep track of the mail and do the threading.

After reviewing SO and google it looks like I should be using a library like django-treebeard or django-mptt to do this. When I review the documentation for either of these two solutions it looks like most of the models are using foreign key relationships and this confuses me.

Given the example model above, how can I implement either django-treebeard or django-mptt into my app? Is this possible using the mesg_id and in_reply_to fields?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:40:40+00:00Added an answer on May 23, 2026 at 2:40 am

    If I were implementing this, I might try it as follows – using django-mptt:

    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'
    

    Note that I’ve turned reply_to into a ForeignKey. This means that if I have a Message instance msg I can simply do msg.reply_to to access the Message instance that it was a reply to, or msg.replies.all() to get all replies to the message.

    In theory, you could use the msg_id as a primary key field. I personally prefer keeping data separate from primary keys, but I don’t know of a particular reason to think my way is better.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Django app that gets it's data completely from an external source
I have a Django app that needs to pull the follower_count information from several
I would like to have a global variable in my django app that stores
I have a Django app that stores client data. Currently, there are just over
I have a Django app that use a django-tagging. I need to port this
This is probably a setting error somewhere. I have a django app that works
I have written a Django app that makes use of Python threading to create
I'm using Google App Engine and Django templates. I have a table that I
I have a Django app that allows the user to create variables and name
I have an existing django web app that is in use. I have to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.