Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在save方法中创建两个对象_Python_Mysql_Django_Django Models - Fatal编程技术网

Python 在save方法中创建两个对象

Python 在save方法中创建两个对象,python,mysql,django,django-models,Python,Mysql,Django,Django Models,我想覆盖一个保存方法,以便为每个保存创建两个对象,而不是一个。我将如何执行以下操作 class Message(models.Model): thread = models.ForeignKey('MessageThread') content = models.CharField(max_length=5000) sender = models.ForeignKey(UserProfile, related_name='message_sender') # f

我想覆盖一个保存方法,以便为每个保存创建两个对象,而不是一个。我将如何执行以下操作

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

执行两个
super
,如果存在自动递增字段或将引发
IntegrityError
,请不要忘记递增pk

def save(self, *args, **kwargs):
    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)

    # instead of Message.objects.create(thread=self.thread, content=self.content, sender=sender, recipient=sender, status='read')
    self.pk +=1
    self.sender = sender
    self.recipient = sender
    self.status = 'read'
    super(Message, self).save(*args, **kwargs)

调用save时,只需将pk设为None并让Django分配pk,而不是增加pk。