django app同时创建auth.user和app user,并将auth.user_id存储在app.user中

发布于 2024-10-05 23:23:51 字数 646 浏览 7 评论 0原文

我有一个应用程序,可以通过 auth.user 和应用程序将用户添加到系统中。保存时我想创建用户,然后获取 auth.user.id 并将其添加到 team_user.id 中。

我一直在尝试下面的代码但一无所获。关于我做错了什么有什么建议吗?请指教。

if request.method == 'POST':
    form = SimpleRegForm(request.POST)
    if form.is_valid():
        #form.save()
        username = form.cleaned_data['username']
        email = form.cleaned_data['email']
        password = form.cleaned_data['password1']
        user = User.objects.create_user(username, email, password)

        new_teammember = form.save(commit=False)
        new_teammember.team_user = request.user # I know this is not right. 
        new_teammember.save()

I have an app that adds a user to the system via auth.user and the app. On save I want to create the user and then get the auth.user.id and add that to a team_user.id.

I've been trying the code below and getting no where. Any suggestions as to what I'm doing wrong? Please advise.

if request.method == 'POST':
    form = SimpleRegForm(request.POST)
    if form.is_valid():
        #form.save()
        username = form.cleaned_data['username']
        email = form.cleaned_data['email']
        password = form.cleaned_data['password1']
        user = User.objects.create_user(username, email, password)

        new_teammember = form.save(commit=False)
        new_teammember.team_user = request.user # I know this is not right. 
        new_teammember.save()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

躲猫猫 2024-10-12 23:23:51

问题是 request.user - 这只是用户。

The issue is the request.user - It's just user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文