Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.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
Python 如何使用django信号访问字段_Python_Django_Django Signals - Fatal编程技术网

Python 如何使用django信号访问字段

Python 如何使用django信号访问字段,python,django,django-signals,Python,Django,Django Signals,我想在Django数据库中保存新记录时检查字段(lightStatusA)的值。我觉得iv'e把这些文件读了10遍,但仍然不知道如何得到这个。以下是我当前的models.py代码: from django.db import models from accounts.models import Customer from django.conf import settings from django.contrib.auth import get_user_model from django.d

我想在Django数据库中保存新记录时检查字段(lightStatusA)的值。我觉得iv'e把这些文件读了10遍,但仍然不知道如何得到这个。以下是我当前的models.py代码:

from django.db import models
from accounts.models import Customer
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db.models.signals import post_save

class Data(models.Model):
    author = models.ForeignKey(get_user_model(),on_delete=models.CASCADE,)
    tempData= models.CharField(max_length=50,blank=True,)
    humidData= models.CharField(max_length=50,blank=True,)
    lightStatusA= models.BooleanField(default=True,) 
    dateTime = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return str(self.author)

def check_light_status(sender, **kwargs):
    if kwargs['created']: #make sure its a new record
        #need logic to grab instance.lightStatusA and check it's value

post_save.connect(check_light_status, sender = Data)

是否有某种方法将此值作为参数传递给信号?

检查灯状态功能可以接受
实例
参数

发件人:

实例
正在保存的实际实例

更新:您说过:

实例返回文章的作者

我将用我的推理能力来猜测你是否试图打印(实例)并看到了作者。查看您的
\uuu str\uu
实现

def __str__(self):
    return str(self.author)
我想说你在那里破坏了自己;)

啊,好的,明白了:

def check_light_status(sender, instance, **kwargs):
    if kwargs['created']: #make sure its a new record
        print(instance.lightStatusA)

这会打印出我需要运行一些逻辑的字段。

我这样做了,但是这个实例返回了文章的作者。不确定调用特定字段的语法?