我的 django 应用程序中有一个信号,我想检查模型中的某个字段是否已更新,以便我可以继续做一些事情。
我的模型看起来像这样......
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.PositiveIntegerField()
tax_rate = models.PositiveIntegerField()
display_price = models.PositiveInteger()
inputed_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL)
updated_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, on_delete=models.SET_NULL)
Run Code Online (Sandbox Code Playgroud)
我的信号是这样的...
@receiver(post_save, sender=Product)
def update_model(sender, **kwargs):
instance = kwargs['instance']
if 'tax_rate' in kwargs['update_fields']:
# do something
Run Code Online (Sandbox Code Playgroud)
这将返回错误None不是可迭代的。我已经阅读了关于 django 信号的文档update_fields,它说The set of fields to update as passed to Model.save(), or None if update_fields wasn’t passed to save().
我应该提到我在 django admin 中工作,所以我希望发生的是,我可以在 django admin 中创建我的 Product 模型的实例,然后如果 …
我正在尝试扩展内置用户并向其添加更多信息。我的 django 项目中有两个应用程序-general 和 user_details。在我的 user_details 应用程序中,在 models.py 文件中,我有以下内容...
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from general.models import Ward
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
firstname = models.CharField(max_length=20)
middlename = models.CharField(max_length=20)
lastname = models.CharField(max_length=20)
loc_id = models.CharField(max_length=8, unique=True)
ward = models.ForeignKey(Ward, related_name="wards", on_delete=models.CASCADE)
phone = models.CharField(max_length=10, unique=True)
address = models.CharField(max_length=255)
postal_code = models.CharField(max_length=15)
def __str__(self):
return "{} {}".format(self.user.first_name, self.user.last_name)
class Meta:
verbose_name_plural = "User details"
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过内置的 django 管理员添加一个新用户,但我不断收到此错误。即使尝试通过终端创建超级用户也会出现相同的错误。与用户无关的所有其他模型都可以正常工作。我的models.py 文件中的代码如下。
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.core.exceptions import ObjectDoesNotExist
from general.models import Ward
# Create your models here.
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
firstname = models.CharField(max_length=20)
middlename = models.CharField(max_length=20)
lastname = models.CharField(max_length=20)
loc_id = models.CharField(max_length=8, unique=True)
ward = models.ForeignKey(Ward, related_name="wards", null=True, on_delete=models.SET_NULL)
phone = models.CharField(max_length=10, unique=True)
address = models.CharField(max_length=255)
postal_code = models.CharField(max_length=15)
def __str__(self):
return "{} {}".format(self.user.first_name, self.user.last_name)
class Meta:
verbose_name_plural = "Personal information" …Run Code Online (Sandbox Code Playgroud)