2015-11-23 18:46:19 +08:00
|
|
|
import json
|
2014-10-21 05:29:28 +08:00
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
from django import forms
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.admin.utils import (
|
|
|
|
display_for_field, flatten_fieldsets, help_text_for_field, label_for_field,
|
|
|
|
lookup_field,
|
|
|
|
)
|
2009-12-23 02:29:00 +08:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from django.db.models.fields.related import ManyToManyRel
|
2013-09-17 00:52:05 +08:00
|
|
|
from django.forms.utils import flatatt
|
2014-03-23 00:00:50 +08:00
|
|
|
from django.template.defaultfilters import capfirst, linebreaksbr
|
2012-07-03 07:31:14 +08:00
|
|
|
from django.utils.html import conditional_escape, format_html
|
2009-12-23 02:29:00 +08:00
|
|
|
from django.utils.safestring import mark_safe
|
2017-01-27 03:58:33 +08:00
|
|
|
from django.utils.translation import gettext, gettext_lazy as _
|
2009-12-23 02:29:00 +08:00
|
|
|
|
2009-03-24 04:22:56 +08:00
|
|
|
ACTION_CHECKBOX_NAME = '_selected_action'
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2009-03-24 04:22:56 +08:00
|
|
|
class ActionForm(forms.Form):
|
|
|
|
action = forms.ChoiceField(label=_('Action:'))
|
2016-03-29 06:33:29 +08:00
|
|
|
select_across = forms.BooleanField(
|
|
|
|
label='',
|
|
|
|
required=False,
|
|
|
|
initial=0,
|
|
|
|
widget=forms.HiddenInput({'class': 'select-across'}),
|
|
|
|
)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2009-03-24 04:22:56 +08:00
|
|
|
checkbox = forms.CheckboxInput({'class': 'action-select'}, lambda value: False)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class AdminForm:
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, form, fieldsets, prepopulated_fields, readonly_fields=None, model_admin=None):
|
2013-12-07 22:52:30 +08:00
|
|
|
self.form, self.fieldsets = form, fieldsets
|
2008-08-18 23:49:58 +08:00
|
|
|
self.prepopulated_fields = [{
|
|
|
|
'field': form[field_name],
|
|
|
|
'dependencies': [form[f] for f in dependencies]
|
|
|
|
} for field_name, dependencies in prepopulated_fields.items()]
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
|
|
|
if readonly_fields is None:
|
|
|
|
readonly_fields = ()
|
|
|
|
self.readonly_fields = readonly_fields
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for name, options in self.fieldsets:
|
2013-12-09 01:20:06 +08:00
|
|
|
yield Fieldset(
|
|
|
|
self.form, name,
|
2009-12-23 02:29:00 +08:00
|
|
|
readonly_fields=self.readonly_fields,
|
|
|
|
model_admin=self.model_admin,
|
|
|
|
**options
|
|
|
|
)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
2016-09-27 21:56:19 +08:00
|
|
|
@property
|
|
|
|
def errors(self):
|
|
|
|
return self.form.errors
|
|
|
|
|
|
|
|
@property
|
|
|
|
def non_field_errors(self):
|
|
|
|
return self.form.non_field_errors
|
|
|
|
|
2016-08-26 08:06:22 +08:00
|
|
|
@property
|
|
|
|
def media(self):
|
2008-08-18 23:49:58 +08:00
|
|
|
media = self.form.media
|
|
|
|
for fs in self:
|
|
|
|
media = media + fs.media
|
|
|
|
return media
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Fieldset:
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, form, name=None, readonly_fields=(), fields=(), classes=(),
|
2016-03-29 06:33:29 +08:00
|
|
|
description=None, model_admin=None):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.form = form
|
|
|
|
self.name, self.fields = name, fields
|
2012-06-08 00:08:47 +08:00
|
|
|
self.classes = ' '.join(classes)
|
2008-08-18 23:49:58 +08:00
|
|
|
self.description = description
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
|
|
|
self.readonly_fields = readonly_fields
|
2008-08-18 23:49:58 +08:00
|
|
|
|
2016-08-26 08:06:22 +08:00
|
|
|
@property
|
|
|
|
def media(self):
|
2008-08-18 23:49:58 +08:00
|
|
|
if 'collapse' in self.classes:
|
2012-02-05 18:26:05 +08:00
|
|
|
extra = '' if settings.DEBUG else '.min'
|
2016-03-29 06:33:29 +08:00
|
|
|
js = [
|
|
|
|
'vendor/jquery/jquery%s.js' % extra,
|
|
|
|
'jquery.init.js',
|
|
|
|
'collapse%s.js' % extra,
|
|
|
|
]
|
2015-11-07 19:24:38 +08:00
|
|
|
return forms.Media(js=['admin/js/%s' % url for url in js])
|
2008-08-18 23:49:58 +08:00
|
|
|
return forms.Media()
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for field in self.fields:
|
2009-12-23 02:29:00 +08:00
|
|
|
yield Fieldline(self.form, field, self.readonly_fields, model_admin=self.model_admin)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Fieldline:
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, form, field, readonly_fields=None, model_admin=None):
|
2013-11-03 05:02:56 +08:00
|
|
|
self.form = form # A django.forms.Form instance
|
2016-12-29 23:27:49 +08:00
|
|
|
if not hasattr(field, "__iter__") or isinstance(field, str):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.fields = [field]
|
|
|
|
else:
|
|
|
|
self.fields = field
|
2016-03-29 06:33:29 +08:00
|
|
|
self.has_visible_field = not all(
|
|
|
|
field in self.form.fields and self.form.fields[field].widget.is_hidden
|
|
|
|
for field in self.fields
|
|
|
|
)
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
|
|
|
if readonly_fields is None:
|
|
|
|
readonly_fields = ()
|
|
|
|
self.readonly_fields = readonly_fields
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for i, field in enumerate(self.fields):
|
2009-12-23 02:29:00 +08:00
|
|
|
if field in self.readonly_fields:
|
2016-03-29 06:33:29 +08:00
|
|
|
yield AdminReadonlyField(self.form, field, is_first=(i == 0), model_admin=self.model_admin)
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
|
|
|
yield AdminField(self.form, field, is_first=(i == 0))
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def errors(self):
|
2014-09-04 20:15:09 +08:00
|
|
|
return mark_safe(
|
2016-03-29 06:33:29 +08:00
|
|
|
'\n'.join(
|
|
|
|
self.form[f].errors.as_ul() for f in self.fields if f not in self.readonly_fields
|
|
|
|
).strip('\n')
|
2014-09-04 20:15:09 +08:00
|
|
|
)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class AdminField:
|
2008-08-18 23:49:58 +08:00
|
|
|
def __init__(self, form, field, is_first):
|
2013-11-03 05:02:56 +08:00
|
|
|
self.field = form[field] # A django.forms.BoundField instance
|
|
|
|
self.is_first = is_first # Whether this field is first on the line
|
2008-08-18 23:49:58 +08:00
|
|
|
self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput)
|
2014-04-15 05:18:03 +08:00
|
|
|
self.is_readonly = False
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def label_tag(self):
|
|
|
|
classes = []
|
2017-03-04 22:47:49 +08:00
|
|
|
contents = conditional_escape(self.field.label)
|
2008-08-18 23:49:58 +08:00
|
|
|
if self.is_checkbox:
|
2012-06-08 00:08:47 +08:00
|
|
|
classes.append('vCheckboxLabel')
|
2013-07-27 02:43:46 +08:00
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
if self.field.field.required:
|
2012-06-08 00:08:47 +08:00
|
|
|
classes.append('required')
|
2008-08-18 23:49:58 +08:00
|
|
|
if not self.is_first:
|
2012-06-08 00:08:47 +08:00
|
|
|
classes.append('inline')
|
2013-05-27 10:47:50 +08:00
|
|
|
attrs = {'class': ' '.join(classes)} if classes else {}
|
2013-07-27 02:43:46 +08:00
|
|
|
# checkboxes should not have a label suffix as the checkbox appears
|
|
|
|
# to the left of the label.
|
2016-03-29 06:33:29 +08:00
|
|
|
return self.field.label_tag(
|
|
|
|
contents=mark_safe(contents), attrs=attrs,
|
|
|
|
label_suffix='' if self.is_checkbox else None,
|
|
|
|
)
|
2008-08-18 23:49:58 +08:00
|
|
|
|
2010-12-21 23:07:09 +08:00
|
|
|
def errors(self):
|
|
|
|
return mark_safe(self.field.errors.as_ul())
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class AdminReadonlyField:
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, form, field, is_first, model_admin=None):
|
2010-04-05 23:37:43 +08:00
|
|
|
# Make self.field look a little bit like a field. This means that
|
|
|
|
# {{ field.name }} must be a useful class name to identify the field.
|
|
|
|
# For convenience, store other field-related data here too.
|
2010-04-11 16:35:04 +08:00
|
|
|
if callable(field):
|
2013-05-27 10:47:50 +08:00
|
|
|
class_name = field.__name__ if field.__name__ != '<lambda>' else ''
|
2010-04-11 16:35:04 +08:00
|
|
|
else:
|
|
|
|
class_name = field
|
2014-02-19 06:44:33 +08:00
|
|
|
|
|
|
|
if form._meta.labels and class_name in form._meta.labels:
|
|
|
|
label = form._meta.labels[class_name]
|
|
|
|
else:
|
|
|
|
label = label_for_field(field, form._meta.model, model_admin)
|
|
|
|
|
|
|
|
if form._meta.help_texts and class_name in form._meta.help_texts:
|
|
|
|
help_text = form._meta.help_texts[class_name]
|
|
|
|
else:
|
|
|
|
help_text = help_text_for_field(class_name, form._meta.model)
|
|
|
|
|
2010-04-05 23:37:43 +08:00
|
|
|
self.field = {
|
2010-04-11 16:35:04 +08:00
|
|
|
'name': class_name,
|
2010-04-05 23:37:43 +08:00
|
|
|
'label': label,
|
2014-02-19 06:44:33 +08:00
|
|
|
'help_text': help_text,
|
2010-04-05 23:37:43 +08:00
|
|
|
'field': field,
|
|
|
|
}
|
2009-12-23 02:29:00 +08:00
|
|
|
self.form = form
|
|
|
|
self.model_admin = model_admin
|
|
|
|
self.is_first = is_first
|
|
|
|
self.is_checkbox = False
|
|
|
|
self.is_readonly = True
|
2015-03-13 18:08:03 +08:00
|
|
|
self.empty_value_display = model_admin.get_empty_value_display()
|
2009-12-23 02:29:00 +08:00
|
|
|
|
|
|
|
def label_tag(self):
|
|
|
|
attrs = {}
|
|
|
|
if not self.is_first:
|
|
|
|
attrs["class"] = "inline"
|
2010-04-11 16:35:04 +08:00
|
|
|
label = self.field['label']
|
2017-03-04 22:47:49 +08:00
|
|
|
return format_html('<label{}>{}:</label>', flatatt(attrs), capfirst(label))
|
2009-12-23 02:29:00 +08:00
|
|
|
|
|
|
|
def contents(self):
|
|
|
|
from django.contrib.admin.templatetags.admin_list import _boolean_icon
|
2010-04-05 23:37:43 +08:00
|
|
|
field, obj, model_admin = self.field['field'], self.form.instance, self.model_admin
|
2009-12-23 02:29:00 +08:00
|
|
|
try:
|
|
|
|
f, attr, value = lookup_field(field, obj, model_admin)
|
2010-03-21 22:23:25 +08:00
|
|
|
except (AttributeError, ValueError, ObjectDoesNotExist):
|
2015-03-13 18:08:03 +08:00
|
|
|
result_repr = self.empty_value_display
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
|
|
|
if f is None:
|
2018-01-04 02:24:02 +08:00
|
|
|
if getattr(attr, 'boolean', False):
|
2009-12-23 02:29:00 +08:00
|
|
|
result_repr = _boolean_icon(value)
|
|
|
|
else:
|
2015-09-09 03:46:26 +08:00
|
|
|
if hasattr(value, "__html__"):
|
|
|
|
result_repr = value
|
2014-03-23 00:00:50 +08:00
|
|
|
else:
|
2017-03-04 22:47:49 +08:00
|
|
|
result_repr = linebreaksbr(value)
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
2015-02-26 22:19:17 +08:00
|
|
|
if isinstance(f.remote_field, ManyToManyRel) and value is not None:
|
2016-12-29 23:27:49 +08:00
|
|
|
result_repr = ", ".join(map(str, value.all()))
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
2015-03-13 18:08:03 +08:00
|
|
|
result_repr = display_for_field(value, f, self.empty_value_display)
|
2015-12-25 05:32:53 +08:00
|
|
|
result_repr = linebreaksbr(result_repr)
|
2009-12-23 02:29:00 +08:00
|
|
|
return conditional_escape(result_repr)
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class InlineAdminFormSet:
|
2008-08-18 23:49:58 +08:00
|
|
|
"""
|
|
|
|
A wrapper around an inline formset for use in the admin system.
|
|
|
|
"""
|
2011-04-22 20:02:25 +08:00
|
|
|
def __init__(self, inline, formset, fieldsets, prepopulated_fields=None,
|
2016-03-29 06:33:29 +08:00
|
|
|
readonly_fields=None, model_admin=None):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.opts = inline
|
|
|
|
self.formset = formset
|
|
|
|
self.fieldsets = fieldsets
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
|
|
|
if readonly_fields is None:
|
|
|
|
readonly_fields = ()
|
|
|
|
self.readonly_fields = readonly_fields
|
2011-04-22 20:02:25 +08:00
|
|
|
if prepopulated_fields is None:
|
|
|
|
prepopulated_fields = {}
|
|
|
|
self.prepopulated_fields = prepopulated_fields
|
2015-11-07 23:46:50 +08:00
|
|
|
self.classes = ' '.join(inline.classes) if inline.classes else ''
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
for form, original in zip(self.formset.initial_forms, self.formset.get_queryset()):
|
2013-10-24 23:28:09 +08:00
|
|
|
view_on_site_url = self.opts.get_view_on_site_url(original)
|
2016-03-29 06:33:29 +08:00
|
|
|
yield InlineAdminForm(
|
|
|
|
self.formset, form, self.fieldsets, self.prepopulated_fields,
|
|
|
|
original, self.readonly_fields, model_admin=self.opts,
|
|
|
|
view_on_site_url=view_on_site_url,
|
|
|
|
)
|
2008-08-18 23:49:58 +08:00
|
|
|
for form in self.formset.extra_forms:
|
2016-03-29 06:33:29 +08:00
|
|
|
yield InlineAdminForm(
|
|
|
|
self.formset, form, self.fieldsets, self.prepopulated_fields,
|
|
|
|
None, self.readonly_fields, model_admin=self.opts,
|
|
|
|
)
|
|
|
|
yield InlineAdminForm(
|
|
|
|
self.formset, self.formset.empty_form,
|
2011-04-22 20:02:25 +08:00
|
|
|
self.fieldsets, self.prepopulated_fields, None,
|
2016-03-29 06:33:29 +08:00
|
|
|
self.readonly_fields, model_admin=self.opts,
|
|
|
|
)
|
2010-01-26 23:02:53 +08:00
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
def fields(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
fk = getattr(self.formset, "fk", None)
|
2013-02-24 01:24:32 +08:00
|
|
|
for i, field_name in enumerate(flatten_fieldsets(self.fieldsets)):
|
|
|
|
if fk and fk.name == field_name:
|
2008-11-01 06:07:05 +08:00
|
|
|
continue
|
2013-02-24 01:24:32 +08:00
|
|
|
if field_name in self.readonly_fields:
|
2010-04-30 23:24:07 +08:00
|
|
|
yield {
|
2013-02-24 01:24:32 +08:00
|
|
|
'label': label_for_field(field_name, self.opts.model, self.opts),
|
2016-03-29 06:33:29 +08:00
|
|
|
'widget': {'is_hidden': False},
|
2013-02-24 01:24:32 +08:00
|
|
|
'required': False,
|
|
|
|
'help_text': help_text_for_field(field_name, self.opts.model),
|
2010-04-30 23:24:07 +08:00
|
|
|
}
|
2009-12-23 02:29:00 +08:00
|
|
|
else:
|
2016-06-19 10:03:25 +08:00
|
|
|
form_field = self.formset.empty_form.fields[field_name]
|
2015-10-07 07:01:39 +08:00
|
|
|
label = form_field.label
|
|
|
|
if label is None:
|
|
|
|
label = label_for_field(field_name, self.opts.model, self.opts)
|
|
|
|
yield {
|
|
|
|
'label': label,
|
|
|
|
'widget': form_field.widget,
|
|
|
|
'required': form_field.required,
|
|
|
|
'help_text': form_field.help_text,
|
|
|
|
}
|
2008-08-18 23:49:58 +08:00
|
|
|
|
2015-11-23 18:46:19 +08:00
|
|
|
def inline_formset_data(self):
|
|
|
|
verbose_name = self.opts.verbose_name
|
|
|
|
return json.dumps({
|
|
|
|
'name': '#%s' % self.formset.prefix,
|
|
|
|
'options': {
|
|
|
|
'prefix': self.formset.prefix,
|
2017-01-27 03:58:33 +08:00
|
|
|
'addText': gettext('Add another %(verbose_name)s') % {
|
2015-11-23 18:46:19 +08:00
|
|
|
'verbose_name': capfirst(verbose_name),
|
|
|
|
},
|
2017-01-27 03:58:33 +08:00
|
|
|
'deleteText': gettext('Remove'),
|
2015-11-23 18:46:19 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-09-27 21:56:19 +08:00
|
|
|
@property
|
|
|
|
def forms(self):
|
|
|
|
return self.formset.forms
|
|
|
|
|
|
|
|
@property
|
|
|
|
def non_form_errors(self):
|
|
|
|
return self.formset.non_form_errors
|
|
|
|
|
2016-08-26 08:06:22 +08:00
|
|
|
@property
|
|
|
|
def media(self):
|
2017-12-01 04:19:02 +08:00
|
|
|
media = self.opts.media + self.formset.media
|
2008-08-18 23:49:58 +08:00
|
|
|
for fs in self:
|
|
|
|
media = media + fs.media
|
|
|
|
return media
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
class InlineAdminForm(AdminForm):
|
|
|
|
"""
|
|
|
|
A wrapper around an inline form for use in the admin system.
|
|
|
|
"""
|
2009-12-23 02:29:00 +08:00
|
|
|
def __init__(self, formset, form, fieldsets, prepopulated_fields, original,
|
2016-03-29 06:33:29 +08:00
|
|
|
readonly_fields=None, model_admin=None, view_on_site_url=None):
|
2008-08-18 23:49:58 +08:00
|
|
|
self.formset = formset
|
2009-12-23 02:29:00 +08:00
|
|
|
self.model_admin = model_admin
|
2008-08-18 23:49:58 +08:00
|
|
|
self.original = original
|
2013-10-24 23:28:09 +08:00
|
|
|
self.show_url = original and view_on_site_url is not None
|
|
|
|
self.absolute_url = view_on_site_url
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(form, fieldsets, prepopulated_fields, readonly_fields, model_admin)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
def __iter__(self):
|
|
|
|
for name, options in self.fieldsets:
|
2016-03-29 06:33:29 +08:00
|
|
|
yield InlineFieldset(
|
|
|
|
self.formset, self.form, name, self.readonly_fields,
|
|
|
|
model_admin=self.model_admin, **options
|
|
|
|
)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2013-07-20 03:55:16 +08:00
|
|
|
def needs_explicit_pk_field(self):
|
2018-01-04 02:24:02 +08:00
|
|
|
return (
|
|
|
|
# Auto fields are editable, so check for auto or non-editable pk.
|
|
|
|
self.form._meta.model._meta.auto_field or not self.form._meta.model._meta.pk.editable or
|
|
|
|
# Also search any parents for an auto field. (The pk info is
|
|
|
|
# propagated to child models so that does not need to be checked
|
|
|
|
# in parents.)
|
|
|
|
any(parent._meta.auto_field or not parent._meta.model._meta.pk.editable
|
|
|
|
for parent in self.form._meta.model._meta.get_parent_list())
|
2017-12-27 02:44:12 +08:00
|
|
|
)
|
2009-05-03 21:37:20 +08:00
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
def pk_field(self):
|
2008-08-31 17:49:55 +08:00
|
|
|
return AdminField(self.form, self.formset._pk_field.name, False)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
def fk_field(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
fk = getattr(self.formset, "fk", None)
|
|
|
|
if fk:
|
|
|
|
return AdminField(self.form, fk.name, False)
|
|
|
|
else:
|
|
|
|
return ""
|
2008-08-18 23:49:58 +08:00
|
|
|
|
|
|
|
def deletion_field(self):
|
|
|
|
from django.forms.formsets import DELETION_FIELD_NAME
|
|
|
|
return AdminField(self.form, DELETION_FIELD_NAME, False)
|
|
|
|
|
|
|
|
def ordering_field(self):
|
|
|
|
from django.forms.formsets import ORDERING_FIELD_NAME
|
|
|
|
return AdminField(self.form, ORDERING_FIELD_NAME, False)
|
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
class InlineFieldset(Fieldset):
|
|
|
|
def __init__(self, formset, *args, **kwargs):
|
|
|
|
self.formset = formset
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(*args, **kwargs)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2008-11-01 06:07:05 +08:00
|
|
|
def __iter__(self):
|
2008-11-14 03:03:44 +08:00
|
|
|
fk = getattr(self.formset, "fk", None)
|
2008-11-01 06:07:05 +08:00
|
|
|
for field in self.fields:
|
2008-11-14 03:03:44 +08:00
|
|
|
if fk and fk.name == field:
|
2008-11-01 06:07:05 +08:00
|
|
|
continue
|
2016-03-29 06:33:29 +08:00
|
|
|
yield Fieldline(self.form, field, self.readonly_fields, model_admin=self.model_admin)
|
2009-03-24 04:22:56 +08:00
|
|
|
|
2013-10-31 23:42:28 +08:00
|
|
|
|
2013-09-17 00:52:05 +08:00
|
|
|
class AdminErrorList(forms.utils.ErrorList):
|
2017-01-25 04:31:57 +08:00
|
|
|
"""Store errors for the form/formsets in an add/change view."""
|
2008-08-18 23:49:58 +08:00
|
|
|
def __init__(self, form, inline_formsets):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2013-11-30 03:38:13 +08:00
|
|
|
|
2008-08-18 23:49:58 +08:00
|
|
|
if form.is_bound:
|
2015-04-17 22:25:11 +08:00
|
|
|
self.extend(form.errors.values())
|
2008-08-18 23:49:58 +08:00
|
|
|
for inline_formset in inline_formsets:
|
|
|
|
self.extend(inline_formset.non_form_errors())
|
|
|
|
for errors_in_inline_form in inline_formset.errors:
|
2015-04-17 22:25:11 +08:00
|
|
|
self.extend(errors_in_inline_form.values())
|