def get_form self request obj None kwargs Overwrite get_form to select

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def get_form(self, request, obj=None, **kwargs):
'''
Overwrite get_form to select the currently logged in user as the author
'''
form = super(PostAdmin, self).get_form(request, obj, **kwargs)
user = request.user
if user.is_superuser:
f = form.base_fields['author']
f.queryset = User.objects.filter(is_staff=True)
if not f.initial:
f.initial=request.user.pk
else:
form.base_fields['author'] = forms.CharField(initial=request.user.pk,
widget=forms.HiddenInput())
return form