本文整理汇总了Python中django.contrib.admin.ModelAdmin.get_form方法的典型用法代码示例。如果您正苦于以下问题:Python ModelAdmin.get_form方法的具体用法?Python ModelAdmin.get_form怎么用?Python ModelAdmin.get_form使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.admin.ModelAdmin
的用法示例。
在下文中一共展示了ModelAdmin.get_form方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_form
# 需要导入模块: from django.contrib.admin import ModelAdmin [as 别名]
# 或者: from django.contrib.admin.ModelAdmin import get_form [as 别名]
def get_form(self, request, obj=None, **kwargs):
if request.method == 'POST' and obj is None:
try:
request.POST['dispatcher'] = Dispatcher.objects.get(name__iexact=u' '.join((request.user.last_name, request.user.first_name)))
except:
pass
return ModelAdmin.get_form(self, request, obj=obj, **kwargs)
示例2: test_admin_forms
# 需要导入模块: from django.contrib.admin import ModelAdmin [as 别名]
# 或者: from django.contrib.admin.ModelAdmin import get_form [as 别名]
def test_admin_forms(self):
site = AdminSite()
model_admin = ModelAdmin(IntModel, site)
form_clazz = model_admin.get_form(None)
form_instance = form_clazz()
try:
form_instance.as_table()
except TypeError:
self.fail('HTML Rendering of the form caused a TypeError')
示例3: app_settings
# 需要导入模块: from django.contrib.admin import ModelAdmin [as 别名]
# 或者: from django.contrib.admin.ModelAdmin import get_form [as 别名]
def app_settings(self, request, app_name):
model_classes = registered_settings.get(app_name)
if not model_classes:
raise Http404
forms = []
fieldsets = []
model_admin = ModelAdmin(Root, self)
for model_name, model in model_classes.items():
instance = getattr(settings, '%s_%s' % (app_name, model_name))
model_admin = self.registered_settings.get(model)
if not model_admin:
model_admin = ModelAdmin(model, self)
form_class = model_admin.get_form(request, instance)
form = form_class(
prefix=model_name,
data=request.POST or None,
instance=instance
)
forms.append(form)
meta = getattr(model, '_meta')
name = '%s %s' % (app_name.title(), meta.verbose_name)
readonly_fields = model_admin.readonly_fields
fields = list(form.fields.keys()) + list(readonly_fields)
fs = Fieldset(form, name, readonly_fields, fields)
fieldsets.append(fs)
# list comprehension to evaluate all forms
if all([form.is_valid() for form in forms]):
for form in forms:
form.save()
msg = '%s settings have been saved.' % app_name.title()
messages.info(request, msg)
if '_save' in request.POST:
url = reverse('admin:settings', current_app=self.name)
return redirect(url)
return redirect(request.get_full_path())
context = {
'app_name': app_name.title(),
'forms': forms,
'fieldsets': fieldsets,
'opts': FakeOpts(),
'change': True,
'is_popup': False,
'save_as': False,
'has_add_permission': False,
'has_delete_permission': False,
'has_change_permission': True,
'media': model_admin.media,
}
return TemplateResponse(
request,
'admin/dbsettings/app_settings.html',
context,
current_app=self.name
)
示例4: get_form
# 需要导入模块: from django.contrib.admin import ModelAdmin [as 别名]
# 或者: from django.contrib.admin.ModelAdmin import get_form [as 别名]
def get_form(self, request, obj=None, **kwargs):
"""
Push request into the ModelForm. Requires the form to override __init__
and remove request from the kwargs.
See: http://stackoverflow.com/questions/1057252/how-do-i-access-the-request-object-or-any-other-variable-in-a-forms-clean-met
"""
ModelForm = ModelAdmin.get_form(self, request, obj, **kwargs)
class ModelFormWithRequestWrapper(ModelForm):
"""class wrapping actual model form class to capture request"""
def __new__(cls, *args, **kwargs):
kwargs['request'] = request
return ModelForm(*args, **kwargs)
return ModelFormWithRequestWrapper
示例5: test_confirm_email
# 需要导入模块: from django.contrib.admin import ModelAdmin [as 别名]
# 或者: from django.contrib.admin.ModelAdmin import get_form [as 别名]
def test_confirm_email(self):
ecla_admin = ModelAdmin(EmailConfirmation, self.site)
ecla_form = ecla_admin.get_form(request)
self.assertIn('email_field_name', ecla_form.base_fields.keys())
self.assertIn('email', ecla_form.base_fields.keys())