1 | from django.contrib import admin
|
---|
2 |
|
---|
3 | from .models import Post
|
---|
4 |
|
---|
5 | class PostAdmin(admin.ModelAdmin):
|
---|
6 | prepopulated_fields = {'slug': ('title',)}
|
---|
7 |
|
---|
8 | fieldsets = (
|
---|
9 | (None, {'fields': (('title', 'slug'), 'text')}),
|
---|
10 | ('Publishing options', {'fields': (('public', 'author'),
|
---|
11 | ('pub_date', 'last_updated'))})
|
---|
12 | )
|
---|
13 |
|
---|
14 | list_display = ('title',)
|
---|
15 |
|
---|
16 | def save_model(self, request, obj, form, change):
|
---|
17 | if not change and not form.cleaned_data.get('author'):
|
---|
18 | obj.author = request.user
|
---|
19 | obj.save()
|
---|
20 |
|
---|
21 | def get_form(self, request, obj=None, **kwargs):
|
---|
22 | form = super(PostAdmin, self).get_form(request, obj, **kwargs)
|
---|
23 | form.base_fields['author'].required = False
|
---|
24 | return form
|
---|
25 |
|
---|
26 | admin.site.register(Post, PostAdmin)
|
---|