python - 如何在信号中的模型中传递实例?
问题描述
我有以下模型:
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
education = models.CharField(blank=True, null=True, max_length=255)
country = models.CharField(blank=True, null=True, max_length=255)
facebook = models.CharField(blank=True, null=True, max_length=255)
whatsapp = models.CharField(blank=True, null=True, max_length=255)
description = models.TextField(blank=True)
我正在创建一个信号,在保存用户后,它会创建一个与之关联的配置文件,下面的代码是我的信号。不知道怎么instance
进去 Profile.objects.create
我认为像instance.education
,instance.country
...这样的传递实例是错误的,或者这个信号中有其他错误,因为我收到了这个错误User has no profile
。请帮我修一下,谢谢
signals.py
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
print("INSTANCE BELOW:")
print(instance)
if created:
Profile.objects.create(user=instance, education=instance.education,
country=instance.country, facebook=instance.facebook,
whatsapp=instance.whatsapp, description=instance.description)
解决方案
推荐阅读
- aem - 我的问题是关于作者在 aem 的组件对话框中重新排序每个选项卡的能力
- sql - 在分组记录中选择重复记录?
- windows - 相当于Windows中的“转到文件夹窗口”,以访问位于当前目录其他位置的Windows打开提示符中的文件?
- angularjs - 我怎样才能显示
- ibeacon-android - 当 setForegroundBetweenScanPeriod 设置时间长且信标关闭/打开时,会触发大量 Range 事件
- machine-learning - 了解假设检验和进一步行动
- loops - 我应该将请求对象传递给 goroutine 以阻止来自通道的 for-select 循环吗?
- android - 奥利奥未能在频道 null 上发布通知
- python - 如何使用相同的虚拟环境运行 Python 和 Jupyter,使用 Visual Studio Code
- asp.net - 写入枚举类型的sql server?DropDownListFor| ASP.NET