Django 1-1 关系如何

发布于 2024-10-18 18:00:17 字数 107 浏览 3 评论 0原文

profile = UserProfile.objects.get(....)

我尝试做的 - 是获取当前登录用户的个人资料。我应该在括号里写什么?

profile = UserProfile.objects.get(....)

what i try to do - is to get profile for the currently logged in user. What should i put in the brackets?

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

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

发布评论

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

评论(2

二手情话 2024-10-25 18:00:17

假设您遵循此处描述的模式:

http ://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

您应该能够使用以下内容:

def my_view(request):
    user = request.user
    if not isinstance(user, AnonymousUser):
        profile = user.get_profile()
        # do something with the profile here
    else:
        # handle anonymous users

Assuming you are following the pattern described here:

http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users

You should be able to use the following:

def my_view(request):
    user = request.user
    if not isinstance(user, AnonymousUser):
        profile = user.get_profile()
        # do something with the profile here
    else:
        # handle anonymous users
涙—继续流 2024-10-25 18:00:17

普通人,没必要这么严厉……有些人实际上不知道灰色轮廓勾号是你应该按的。

UserProfile.objects.get(user=request.user) 

但如果它是一个 OneToOne 字段,您应该能够执行 request.user.userprofile
http://docs.djangoproject.com/ en/dev/topics/db/queries/#one-to-one-relationships

Comon guys, no need to be so harsh... Some people actually don't know that the grey outline checkmark is what you're supposed to press.

UserProfile.objects.get(user=request.user) 

But if it's a OneToOne field, you should be able to do request.user.userprofile
http://docs.djangoproject.com/en/dev/topics/db/queries/#one-to-one-relationships

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