好的,这是瘦的:
# models.py
class Article( models.Model ):
title = models.CharField( max_length = 255 )
author = models.ForeignKey( User )
published_at = models.DateTimeField( auto_now_add = True )
body = models.TextField( )
def __unicode__( self ):
return self.title
# admin.py
from hpccoe.news.models import Article
from django.contrib import admin
from django import forms
from django.forms import widgets
class ArticleAdminForm( forms.ModelForm ):
title = forms.CharField( max_length = 255, required = True )
body = forms.CharField( required = True, widget = widgets.Textarea )
class ArticleAdmin( admin.ModelAdmin ):
fields = [ 'title', 'body' ]
form = ArticleAdminForm
admin.site.register( Article, ArticleAdmin )
如您所见,我在管理员表单中省略了作者字段。我想以某种方式在保存之前自动填充它。我该如何做到这一点?最近一个小时我一直在谷歌上,但无济于事。
提前致谢。