中工作
我已经使用django一段时间了,但这不是一个bug,我想知道为什么create_profile方法需要保存概要文件和创建的变量?
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
profile, created = UserProfile.objects.get_or_create(user=instance)
我试过
print >> sys.stderr , "create_user" + str(profile) + (str(created))
并且它们返回User_Profile unicode函数返回值和用于created的布尔值。
我的问题具体是存储配置文件的重要性,创建的值。
UserProfile.objects.get_or_create(user=instance)
我已经尝试过单独调用该语句,它可以在
如果以后要使用它们,通常会这样做:
profile, created = UserProfile.objects.get_or_create(user=instance)
if profle.check_something_here:
return profile.something_else
或者:
profile, created = UserProfile.objects.get_or_create(user=instance)
if created:
# do something with the newly created profile
else:
# do something else if the profile was already there
如果你需要对他们做点什么的话,当然是这样。否则UserProfile.objects.get_or_create(user=instance)
也是正确的。
如果不需要,则无需将调用结果分配给任何变量。所以
UserProfile.objects.get_or_create(user=instance)
很好。
如果您只使用一个变量而不使用另一个变量(根据错误判断):
profile, _ = UserProfile.objects.get_or_create(user=instance)