I want to overwrite a save method so that two objects are created for each save instead of one. How would I do the following?
class Message(models.Model):
thread = models.ForeignKey('MessageThread')
content = models.CharField(max_length=5000)
sender = models.ForeignKey(UserProfile, related_name='message_sender')
# filled in automatically on save()
timestamp = models.DateTimeField(auto_now_add=True)
recipient = models.ForeignKey(UserProfile, related_name='message_recipient')
status = models.CharField(choices=MESSAGE_STATUS, max_length=64, default='unread')
def save(self, *args, **kwargs):
"""
When a message is created, this will save TWO objects of it.
"""
sender = self.sender
thread_initiator = self.thread.initiator
thread_recipient = self.thread.recipient
if sender == thread_recipient:
self.recipient = thread_initiator
else:
self.recipient = thread_recipient
self.status = 'unread'
super(Message, self).save(*args, **kwargs)
# saving the second object creates an error
Message.objects.create(thread=self.thread, content=self.content, sender=sender, recipient=sender, status='read')
--> RuntimeError: maximum recursion depth exceeded while calling a Python object