桃花长相依
我现在将它与元类(而不是方法)一起使用,因为Django 1.3否则会显示一些怪异之处。class MyModelAdmin(admin.ModelAdmin): form = MyCustomForm def get_form(self, request, obj=None, **kwargs): ModelForm = super(MyModelAdmin, self).get_form(request, obj, **kwargs) class ModelFormMetaClass(ModelForm): def __new__(cls, *args, **kwargs): kwargs['request'] = request return ModelForm(*args, **kwargs) return ModelFormMetaClass然后重写MyCustomForm.__init__如下:class MyCustomForm(forms.ModelForm): def __init__(self, *args, **kwargs): self.request = kwargs.pop('request', None) super(MyCustomForm, self).__init__(*args, **kwargs)然后,您可以使用ModelFormwith的任何方法访问请求对象self.request。