2014-09-06 10:56:30 +00:00
|
|
|
from django.contrib.admin import helpers
|
|
|
|
from django.forms.models import modelformset_factory, BaseModelFormSet
|
2014-07-24 15:43:23 +00:00
|
|
|
from django.template import Template, Context
|
|
|
|
|
|
|
|
|
|
|
|
class AdminFormMixin(object):
|
|
|
|
""" Provides a method for rendering a form just like in Django Admin """
|
|
|
|
def as_admin(self):
|
|
|
|
prepopulated_fields = {}
|
|
|
|
fieldsets = [
|
|
|
|
(None, {'fields': self.fields.keys()})
|
|
|
|
]
|
2014-09-06 10:56:30 +00:00
|
|
|
adminform = helpers.AdminForm(self, fieldsets, prepopulated_fields)
|
2014-07-24 15:43:23 +00:00
|
|
|
template = Template(
|
|
|
|
'{% for fieldset in adminform %}'
|
2014-09-02 15:48:07 +00:00
|
|
|
' {% include "admin/includes/fieldset.html" %}'
|
2014-07-24 15:43:23 +00:00
|
|
|
'{% endfor %}'
|
|
|
|
)
|
|
|
|
return template.render(Context({'adminform': adminform}))
|
2014-09-06 10:56:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AdminFormSet(BaseModelFormSet):
|
|
|
|
def as_admin(self):
|
|
|
|
prepopulated = {}
|
|
|
|
fieldsets = [
|
|
|
|
(None, {'fields': self.form().fields.keys()})
|
|
|
|
]
|
|
|
|
readonly = getattr(self.form.Meta, 'readonly_fields', ())
|
|
|
|
if not hasattr(self.modeladmin, 'verbose_name_plural'):
|
|
|
|
opts = self.modeladmin.model._meta
|
|
|
|
self.modeladmin.verbose_name_plural = opts.verbose_name_plural
|
|
|
|
inline_admin_formset = helpers.InlineAdminFormSet(self.modeladmin, self,
|
|
|
|
fieldsets, prepopulated, readonly, model_admin=self.modeladmin)
|
|
|
|
template = Template(
|
|
|
|
'{% include "admin/edit_inline/tabular.html" %}'
|
|
|
|
)
|
|
|
|
return template.render(Context({'inline_admin_formset': inline_admin_formset}))
|
|
|
|
|
|
|
|
|
|
|
|
def adminmodelformset_factory(modeladmin, form):
|
|
|
|
formset = modelformset_factory(modeladmin.model, extra=0,
|
|
|
|
form=form, formset=AdminFormSet)
|
|
|
|
formset.modeladmin = modeladmin
|
|
|
|
return formset
|