在Django管理界面中我想在發佈時自動插入登錄用戶名和博客文章,目前我已經在每個用戶的下拉菜單中顯示每個用戶選擇但顯然這不是很好,所以我想它自動輸入這個。自動插入登錄用戶用戶名在Django
這裏是我的代碼:
models.py
從django.db進口車型 從django.contrib.auth.models導入用戶
# Create your models here.
class Post(models.Model):
title = models.CharField(max_length=200)
body = models.TextField()
published_date = models.DateTimeField('date published')
author = models.ForeignKey(User, db_column="published_who")
def __unicode__(self):
return self.title
admin.py
from blog.models import Post
from django.contrib import admin
class PostAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.author = request.user
obj.save()
admin.site.register(Post, PostAdmin)
非常感謝!