diff options
Diffstat (limited to 'django/forms')
| -rw-r--r-- | django/forms/fields.py | 19 | ||||
| -rw-r--r-- | django/forms/models.py | 89 |
2 files changed, 19 insertions, 89 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index 47ae5e11b2..f3e5528f23 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -442,16 +442,7 @@ class FileField(Field): elif not data and initial: return initial - if isinstance(data, dict): - # We warn once, then support both ways below. - import warnings - warnings.warn( - message = "Representing uploaded files as dictionaries is deprecated. Use django.core.files.uploadedfile.SimpleUploadedFile instead.", - category = DeprecationWarning, - stacklevel = 2 - ) - data = UploadedFile(data['filename'], data['content']) - + # UploadedFile objects should have name and size attributes. try: file_name = data.name file_size = data.size @@ -507,10 +498,10 @@ class ImageField(FileField): # but it must be called immediately after the constructor trial_image = Image.open(file) trial_image.verify() - except ImportError: + except ImportError: # Under PyPy, it is possible to import PIL. However, the underlying - # _imaging C module isn't available, so an ImportError will be - # raised. Catch and re-raise. + # _imaging C module isn't available, so an ImportError will be + # raised. Catch and re-raise. raise except Exception: # Python Imaging Library doesn't recognize it as an image raise ValidationError(self.error_messages['invalid_image']) @@ -643,7 +634,7 @@ class ChoiceField(Field): if value == smart_unicode(k): return True return False - + class MultipleChoiceField(ChoiceField): hidden_widget = MultipleHiddenInput widget = SelectMultiple diff --git a/django/forms/models.py b/django/forms/models.py index 787da8ca40..193dabef7d 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -3,8 +3,6 @@ Helper functions for creating Form classes from Django models and database field objects. """ -from warnings import warn - from django.utils.translation import ugettext_lazy as _ from django.utils.encoding import smart_unicode from django.utils.datastructures import SortedDict @@ -18,8 +16,8 @@ from formsets import BaseFormSet, formset_factory, DELETION_FIELD_NAME __all__ = ( 'ModelForm', 'BaseModelForm', 'model_to_dict', 'fields_for_model', - 'save_instance', 'form_for_model', 'form_for_instance', 'form_for_fields', - 'ModelChoiceField', 'ModelMultipleChoiceField', + 'save_instance', 'form_for_fields', 'ModelChoiceField', + 'ModelMultipleChoiceField', ) def save_instance(form, instance, fields=None, fail_message='saved', @@ -74,65 +72,6 @@ def make_instance_save(instance, fields, fail_message): return save_instance(self, instance, fields, fail_message, commit) return save -def form_for_model(model, form=BaseForm, fields=None, - formfield_callback=lambda f: f.formfield()): - """ - Returns a Form class for the given Django model class. - - Provide ``form`` if you want to use a custom BaseForm subclass. - - Provide ``formfield_callback`` if you want to define different logic for - determining the formfield for a given database field. It's a callable that - takes a database Field instance and returns a form Field instance. - """ - warn("form_for_model is deprecated. Use ModelForm instead.", - PendingDeprecationWarning, stacklevel=3) - opts = model._meta - field_list = [] - for f in opts.fields + opts.many_to_many: - if not f.editable: - continue - if fields and not f.name in fields: - continue - formfield = formfield_callback(f) - if formfield: - field_list.append((f.name, formfield)) - base_fields = SortedDict(field_list) - return type(opts.object_name + 'Form', (form,), - {'base_fields': base_fields, '_model': model, - 'save': make_model_save(model, fields, 'created')}) - -def form_for_instance(instance, form=BaseForm, fields=None, - formfield_callback=lambda f, **kwargs: f.formfield(**kwargs)): - """ - Returns a Form class for the given Django model instance. - - Provide ``form`` if you want to use a custom BaseForm subclass. - - Provide ``formfield_callback`` if you want to define different logic for - determining the formfield for a given database field. It's a callable that - takes a database Field instance, plus **kwargs, and returns a form Field - instance with the given kwargs (i.e. 'initial'). - """ - warn("form_for_instance is deprecated. Use ModelForm instead.", - PendingDeprecationWarning, stacklevel=3) - model = instance.__class__ - opts = model._meta - field_list = [] - for f in opts.fields + opts.many_to_many: - if not f.editable: - continue - if fields and not f.name in fields: - continue - current_value = f.value_from_object(instance) - formfield = formfield_callback(f, initial=current_value) - if formfield: - field_list.append((f.name, formfield)) - base_fields = SortedDict(field_list) - return type(opts.object_name + 'InstanceForm', (form,), - {'base_fields': base_fields, '_model': model, - 'save': make_instance_save(instance, fields, 'changed')}) - def form_for_fields(field_list): """ Returns a Form class for the given list of Django database field instances. @@ -289,7 +228,7 @@ def modelform_factory(model, form=ModelForm, fields=None, exclude=None, setattr(Meta, 'fields', fields) setattr(Meta, 'exclude', exclude) class_name = model.__name__ + 'Form' - return ModelFormMetaclass(class_name, (form,), {'Meta': Meta, + return ModelFormMetaclass(class_name, (form,), {'Meta': Meta, 'formfield_callback': formfield_callback}) @@ -410,7 +349,7 @@ class BaseInlineFormSet(BaseModelFormSet): # is there a better way to get the object descriptor? self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name() super(BaseInlineFormSet, self).__init__(data, files, prefix=prefix or self.rel_name) - + def _construct_forms(self): if self.save_as_new: self._total_form_count = self._initial_form_count @@ -419,7 +358,7 @@ class BaseInlineFormSet(BaseModelFormSet): def get_queryset(self): """ - Returns this FormSet's queryset, but restricted to children of + Returns this FormSet's queryset, but restricted to children of self.instance """ kwargs = {self.fk.name: self.instance} @@ -443,7 +382,7 @@ def _get_foreign_key(parent_model, model, fk_name=None): if len(fks_to_parent) == 1: fk = fks_to_parent[0] if not isinstance(fk, ForeignKey) or \ - (fk.rel.to != parent_model and + (fk.rel.to != parent_model and fk.rel.to not in parent_model._meta.parents.keys()): raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, parent_model)) elif len(fks_to_parent) == 0: @@ -451,9 +390,9 @@ def _get_foreign_key(parent_model, model, fk_name=None): else: # Try to discover what the ForeignKey from model to parent_model is fks_to_parent = [ - f for f in opts.fields - if isinstance(f, ForeignKey) - and (f.rel.to == parent_model + f for f in opts.fields + if isinstance(f, ForeignKey) + and (f.rel.to == parent_model or f.rel.to in parent_model._meta.parents.keys()) ] if len(fks_to_parent) == 1: @@ -478,7 +417,7 @@ def inlineformset_factory(parent_model, model, form=ModelForm, """ fk = _get_foreign_key(parent_model, model, fk_name=fk_name) # let the formset handle object deletion by default - + if exclude is not None: exclude.append(fk.name) else: @@ -528,7 +467,7 @@ class ModelChoiceField(ChoiceField): help_text=None, *args, **kwargs): self.empty_label = empty_label self.cache_choices = cache_choices - + # Call Field instead of ChoiceField __init__() because we don't need # ChoiceField.__init__(). Field.__init__(self, required, widget, label, initial, help_text, @@ -545,8 +484,8 @@ class ModelChoiceField(ChoiceField): queryset = property(_get_queryset, _set_queryset) - # this method will be used to create object labels by the QuerySetIterator. - # Override it to customize the label. + # this method will be used to create object labels by the QuerySetIterator. + # Override it to customize the label. def label_from_instance(self, obj): """ This method is used to convert objects into strings; it's used to @@ -554,7 +493,7 @@ class ModelChoiceField(ChoiceField): can override this method to customize the display of the choices. """ return smart_unicode(obj) - + def _get_choices(self): # If self._choices is set, then somebody must have manually set # the property self.choices. In this case, just return self._choices. |
