Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.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/2/django/23.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/3/apache-spark/5.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 在post_保存信号中,许多字段不可用_Python_Django_Signals - Fatal编程技术网

Python 在post_保存信号中,许多字段不可用

Python 在post_保存信号中,许多字段不可用,python,django,signals,Python,Django,Signals,保存Django模型后,我需要操作该模型中的数据,但我还需要访问ManyToManyField 以下是我想做的: class Lab(Model): institute = ManyToManyField(Institute) def post_save_lab(sender, instance, created, *args, **kwargs): if not instance.institute.all(): # Data processing... post_save

保存Django模型后,我需要操作该模型中的数据,但我还需要访问ManyToManyField

以下是我想做的:

class Lab(Model):
  institute = ManyToManyField(Institute)

def post_save_lab(sender, instance, created, *args, **kwargs):
  if not instance.institute.all():
    # Data processing...

post_save.connect(post_save_lab, sender=Lab)
问题是,在那一刻,instance.institute.all总是空的。。。我怎样才能知道实验室有没有研究

我指定信号m2m_changed不能解决问题,因为如果manytomy关系中没有元素,则必须进行数据处理。因此,不会调用m2m_changed

谢谢

在保存模型实例之前,无法保存m2m。若在post save信号中创建对象时查找m2m实例==True,那个么它将始终为空


我认为您可以使用信号处理程序。

您可以覆盖save方法:

class Lab(Model):
    institute = ManyToManyField(Institute)

    def save(self, *args, **kwargs):
        super(Lab, self).save(*args, **kwargs)
        # ... do something with the many to many
        # example:
        # if self.institute.all().exists():
        #     ...

外键!=曼尼托曼尼菲尔德。这是一种不同类型的关系。可能您应该使用m2m changed信号,因为@Hedde暗示您只有在创建实验室实例或更新时才会看到这种行为?数据处理必须在更新和新对象上完成。您找到解决方案了吗?这并不能解决问题,因为数据处理必须在没有m2m更改的情况下完成…这将不起作用。如果您试图访问示例中的institute manager,它将返回一个空列表,因为关系尚未创建。@0sh您确定吗?最初的save方法首先用super调用。它应该使m2m可用,就像在model类之外保存一个实例,然后在以后添加m2m一样。我同意,这是有道理的,但由于所描述的原因,它不起作用。基本上,m2m关系是在save方法之后更新的。