我有以下管理类:
from django.contrib import admin
class CommandAdmin (admin.ModelAdmin):
list_display = ('name','status','get_requester', 'justification')
readonly_fields = ('name','status','get_requester', 'request', 'justification')
exclude = ('request',)
def get_requester(self,obj):
return obj.request.requester
get_requester.command_order_field = 'requester' #Allows column order sorting
get_requester.short_description = 'Who Requested' #Renames column head
def get_form(self, request, obj=None, **kwargs):
self.exclude = ['request']
form = super(CommandAdmin, self).get_form(request, obj, **kwargs)
return form
def get_queryset(self, request):
print request
qs = super(CommandAdmin, self).get_queryset(request).select_related('request')
return qs.filter(status='pending', request__isnull=False)
Command模型看起来像这样......
class Command(models.Model):
... bunch of fields ...
justification = models.CharField(max_length=2000, blank=True, null=True)
request = models.ForeignKey('sudorequests.Request', blank=True, null=True)
当我到达更改表单时,“请求”对象仍然出现在表单上。这是一个屏幕截图: