用户模型中的访问会话.save()

时间:2014-06-18 08:10:22

标签: django session

是否可以在用户模型中访问当前会话.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)

1 个答案:

答案 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)