i want to make message view show all other messages that led up to that message. the original message will not have a response_to value and should terminate the recursion. is there a better way to do this? (i'm looking at memory over speed, because a thread shouldn't typically be more than 10 - 20 messages long).
def get_thread(msg,msg_set=[]):
"""
This will get all the messages that led up to any particular message
it takes only a message, but if the message isn't the first message
in a thread it appends it to a message list to be returned.
the last message in the list should be the first message created
"""
if msg.response_to:
return get_thread(msg.response_to, msg_set+[msg])
return msg_set+[msg]
# Create your models here.
class Message(models.Model):
body = models.TextField()
sender = models.ForeignKey(User,related_name='sender')
recipients = models.ManyToManyField(User,related_name='recipients')
timestamp = models.DateTimeField(default=datetime.datetime.now)
response_to = models.ForeignKey(Message,related_name='response_to')
def thread(self):
return get_thread(self)