是否可以在用户模型中访问当前会话.save()?
我想要实现的伪代码:
# users.models.py
def save(self, *args, **kwargs):
created = True
if self.pk:
created = False
super(AbstractUser, self).save(*args, **kwargs)
# post-save
if created:
look_for_invite_in_session_and_register_if_found(self, session)
答案 0 :(得分:1)
您的架构似乎有问题。您不应该在模型层中访问请求。所有有要求的工作必须在视野中完成。你可以这样做:
user, created = AbstractUser.objects.get_or_create(name=name)
if created:
look_for_invite_in_session_and_register_if_found(user, request.session)