简体   繁体   English

Django 中用户表的外键

[英]Foreign key to User table in django

I'm using django's built-in contrib.auth module and have setup a foreign key relationship to a User for when a 'post' is added:我正在使用 django 的内置 contrib.auth 模块,并在添加“帖子”时为用户设置了外键关系:

class Post(models.Model):
    owner = models.ForeignKey('User')
    # ... etc.

Now when it comes to actually adding the Post, I'm not sure what to supply in the owner field before calling save().现在当谈到实际添加 Post 时,我不确定在调用 save() 之前在 owner 字段中提供什么。 I expected something like an id in user's session but I noticed User does not have a user_id or id attribute.我期望在用户会话中出现类似 id 的内容,但我注意到 User 没有 user_id 或 id 属性。 What data is it that I should be pulling from the user's authenticated session to populate the owner field with?我应该从用户的经过身份验证的会话中提取哪些数据来填充所有者字段? I've tried to see what's going on in the database table but not too clued up on the sqlite setup yet.我试图查看数据库表中发生了什么,但对 sqlite 设置还不太了解。

Thanks for any help...谢谢你的帮助...

You want to provide a "User" object.您想提供一个“用户”对象。 Ie the same kind of thing you'd get from User.objects.get(pk=13).即你从 User.objects.get(pk=13) 得到的东西。

If you're using the authentication components of Django, the user is also attached to the request object, and you can use it directly from within your view code:如果您使用 Django 的身份验证组件,则用户也会附加到请求对象,您可以直接在视图代码中使用它:

request.user

If the user isn't authenticated, then Django will return an instance of django.contrib.auth.models.AnonymousUser.如果用户未通过身份验证,那么 Django 将返回 django.contrib.auth.models.AnonymousUser 的一个实例。 (per http://docs.djangoproject.com/en/dev/ref/request-response/#attributes ) (每个http://docs.djangoproject.com/en/dev/ref/request-response/#attributes

Requirements --> Django 3, python 3要求 --> Django 3、python 3

1) For add username to owner = models.ForeignKey('User') for save that, in the first step you must add from django.conf import settings above models.py and edit owner = models.ForeignKey('User') to this sample: 1)为了将用户名添加到owner = models.ForeignKey('User')以保存,在第一步中,您必须from django.conf import settings添加models.py上方的from django.conf import settings并编辑owner = models.ForeignKey('User')到这个样本:

class Post(models.Model):
    slug = models.SlugField(max_length=100, unique=True, null=True, allow_unicode=True)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, default=1, on_delete=models.CASCADE)

2) And for show detail Post , special owner name or family or username under the post, you must add the following code in the second step in views.py : 2)并且要在Post显示详细信息Post ,特殊所有者姓名或家庭或用户名,您必须在views.py第二步中添加以下代码:

from django.shortcuts import get_object_or_404
.
.
. 

def detailPost(request,slug=None):
    instance = get_object_or_404(Post, slug=slug)
    context = {
        'instance': instance,
    }
return render(request, template_name='detail_post.html', context=context)

3) And in the third step , you must add the following code for show user information like user full name that creates a post: 3)第三步中,您必须添加以下代码以显示创建帖子的用户全名等用户信息:

<p class="font-small grey-text">Auther: {{ instance.owner.get_full_name  }} </p>

now if you want to use user name , you can use {{ instance.owner.get_username }} or if you want to access short name, you can use {{ instance.owner.get_short_name }} .现在如果你想使用用户名,你可以使用{{ instance.owner.get_username }}或者如果你想访问短名称,你可以使用{{ instance.owner.get_short_name }} See this link for more information.有关更多信息,请参阅此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM