Django 是否有办法只在用户创建时(而不是每次保存时)做某事?
Does Django have a way to do something only at the time of user creation (and not every save)?
我试图解决通过 django allauth 社交登录批量导入用户和用户信息的困难。我通过创建一个单独的模型来做到这一点,我可以将用户信息上传到该模型,该模型将在用户首次登录后移植到真实的 table。本质上,它会根据他们的电子邮件为他们预填充用户信息。我 运行 遇到的问题是,因为我使用的是 post_save
发件人,所以每次用户更改其个人资料的某个方面时,它都会尝试更新信息。我的问题是在用户信息合法更改的情况下,但它没有在 UserData
table 中更新(仅用于初始导入),它只会改回来.我很好奇有什么想法可以解决这个问题。谢谢!
@receiver(post_save, sender=User)
def imported_info_update(sender, instance, **kwargs):
imported_info = UserData.objects.get(email=instance.email)
job = Jobs.objects.get(job=imported_info.department)
location = Locations.objects.get(location=imported_info.team)
school = Schools.objects.get(school=imported_info.school)
UserProfile.objects.update_or_create(user_id=instance.id,
job=job,
location=location,
school=school)
试试这个
@receiver(post_save, sender=User)
def imported_info_update(sender, instance=None, <b>created=False,</b> **kwargs):
<b>if created:</b>
imported_info = UserData.objects.get(email=instance.email)
job = Jobs.objects.get(job=imported_info.department)
location = Locations.objects.get(location=imported_info.team)
school = Schools.objects.get(school=imported_info.school)
UserProfile.objects.update_or_create(user_id=instance.id,
job=job,
location=location,
school=school)
参考:https://docs.djangoproject.com/en/2.0/ref/signals/#post-save
post_save
函数有一个额外的参数:created
[Django-doc]。它是一个布尔值,指定是否已创建对象。你可以这样写:
@receiver(post_save, sender=User)
def imported_info_update(sender, instance, <b>created=None</b>, **kwargs):
<b>if created:</b>
imported_info = UserData.objects.get(email=instance.email)
job = Jobs.objects.get(job=imported_info.department)
location = Locations.objects.get(location=imported_info.team)
school = Schools.objects.get(school=imported_info.school)
UserProfile.objects.update_or_create(user_id=instance.id,
job=job,
location=location,
school=school)
但是请注意,在某些情况下,信号可以被绕过。由于有一些方法可以在不调用 .save()
的情况下构建行。
例如,如果您要使用 Model.objects.bulk_create(collection)
,它 不会 对 collection
的项目调用 .save()
,因此也不会.save()
,触发器也不会触发。
我试图解决通过 django allauth 社交登录批量导入用户和用户信息的困难。我通过创建一个单独的模型来做到这一点,我可以将用户信息上传到该模型,该模型将在用户首次登录后移植到真实的 table。本质上,它会根据他们的电子邮件为他们预填充用户信息。我 运行 遇到的问题是,因为我使用的是 post_save
发件人,所以每次用户更改其个人资料的某个方面时,它都会尝试更新信息。我的问题是在用户信息合法更改的情况下,但它没有在 UserData
table 中更新(仅用于初始导入),它只会改回来.我很好奇有什么想法可以解决这个问题。谢谢!
@receiver(post_save, sender=User)
def imported_info_update(sender, instance, **kwargs):
imported_info = UserData.objects.get(email=instance.email)
job = Jobs.objects.get(job=imported_info.department)
location = Locations.objects.get(location=imported_info.team)
school = Schools.objects.get(school=imported_info.school)
UserProfile.objects.update_or_create(user_id=instance.id,
job=job,
location=location,
school=school)
试试这个
@receiver(post_save, sender=User)
def imported_info_update(sender, instance=None, <b>created=False,</b> **kwargs):
<b>if created:</b>
imported_info = UserData.objects.get(email=instance.email)
job = Jobs.objects.get(job=imported_info.department)
location = Locations.objects.get(location=imported_info.team)
school = Schools.objects.get(school=imported_info.school)
UserProfile.objects.update_or_create(user_id=instance.id,
job=job,
location=location,
school=school)
参考:https://docs.djangoproject.com/en/2.0/ref/signals/#post-save
post_save
函数有一个额外的参数:created
[Django-doc]。它是一个布尔值,指定是否已创建对象。你可以这样写:
@receiver(post_save, sender=User)
def imported_info_update(sender, instance, <b>created=None</b>, **kwargs):
<b>if created:</b>
imported_info = UserData.objects.get(email=instance.email)
job = Jobs.objects.get(job=imported_info.department)
location = Locations.objects.get(location=imported_info.team)
school = Schools.objects.get(school=imported_info.school)
UserProfile.objects.update_or_create(user_id=instance.id,
job=job,
location=location,
school=school)
但是请注意,在某些情况下,信号可以被绕过。由于有一些方法可以在不调用 .save()
的情况下构建行。
例如,如果您要使用 Model.objects.bulk_create(collection)
,它 不会 对 collection
的项目调用 .save()
,因此也不会.save()
,触发器也不会触发。