在Django中获取当前的logged_in用户 [英] Getting current logged_in user in Django
问题描述
这是我的博客模型类:
class博客(models.Model):
PUBLISHING_STATUS =(
('P','发布'),
('D','Draft'),
,'等待批准'),
)
title = models.CharField(max_length = 128,unique = True)
slug = models.SlugField(max_length = 160,unique = True)
header_image = models.ImageField(upload_to ='images / uploaded',max_length = 256,null = True)
body = models.TextField()
author = models.ForeignKey(User)
status = models.CharField(max_length = 2,choices = PUBLISHING_STATUS,null = True)
posted = models.DateTimeField('date posted')
category = models.ForeignKey('blog.Category')
tags = models.ManyToManyField('blog.Tags')
def __unicode __(self):
return self.title
def get_absolute_url(self ):
return'/ blog /%s / view_post'%self.slug
谢谢!
如果您需要该字段,则必须在迁移时设置默认值。由于您没有在任何地方存储任何有关创建该帖子的数据的原因,您只能将其设置为特定用户,在这种情况下,这可能应该是您的管理员用户!
如果需要,您可以编辑管理员中的帖子并将其设置为正确的作者!
如果您正在寻找一种自动填充的方式在管理员中保存新帖子后,您必须覆盖 ModelAdmin
上的 save_model
方法:
class BlogPostAdmin(model_admin):
def save_model(self,request,instance,form,change):
user = request.user
instance = form.save(commit = False)
如果不更改或不是instance.author:
instance.author = user
instance .save()
form.save_m2m()
返回实例
I'm developping a blog with Django, and I'd like my posts to be related to their author. I've created an author field in my Blog model class, which is a ForeignKey pointing on a User object. Problem is, South database migration django tool asks me for a default value to give to it. Actually, I'd like it to be the id of the logged in user who edited the post, but can't see how to make it. Any idea?
This is my Blog model class:
class Blog(models.Model):
PUBLISHING_STATUS = (
('P', 'Published'),
('D', 'Draft'),
('AA', 'Awaiting Approval'),
)
title = models.CharField(max_length=128, unique=True)
slug = models.SlugField(max_length=160, unique=True)
header_image = models.ImageField(upload_to='images/uploaded', max_length=256, null=True)
body = models.TextField()
author = models.ForeignKey(User)
status = models.CharField(max_length=2, choices=PUBLISHING_STATUS, null=True)
posted = models.DateTimeField('date posted')
category = models.ForeignKey('blog.Category')
tags = models.ManyToManyField('blog.Tags')
def __unicode__(self):
return self.title
def get_absolute_url(self):
return '/blog/%s/view_post' % self.slug
Thanks!
If you want the field to be required you will have to set a default value when migrating. For the reason you don't have stored any data anywhere about who created the post, you ca only set it to a specific user, which in this case probably should be your administrator user!
You could then edit the posts in the admin and set them to the correct author, if necessary!
If you are looking for a way to auto-populate the field upon saving a new post in the admin, you have to override the save_model
method on your ModelAdmin
:
class BlogPostAdmin(model_admin):
def save_model(self, request, instance, form, change):
user = request.user
instance = form.save(commit=False)
if not change or not instance.author:
instance.author = user
instance.save()
form.save_m2m()
return instance
这篇关于在Django中获取当前的logged_in用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!