【发布时间】:2009-11-25 15:13:13
【问题描述】:
我想让消息视图显示导致该消息的所有其他消息。原始消息没有 response_to 值,应该终止递归。有一个更好的方法吗? (我看的是内存超过速度,因为一个线程通常不应超过 10 到 20 条消息)。
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)
【问题讨论】: