| 1 | Index: models.py
|
|---|
| 2 | ===================================================================
|
|---|
| 3 | --- models.py (revision 8961)
|
|---|
| 4 | +++ models.py (working copy)
|
|---|
| 5 | @@ -134,8 +134,8 @@
|
|---|
| 6 | fields will be excluded from the returned fields, even if they are listed
|
|---|
| 7 | in the ``fields`` argument.
|
|---|
| 8 | """
|
|---|
| 9 | - # TODO: if fields is provided, it would be nice to return fields in that order
|
|---|
| 10 | field_list = []
|
|---|
| 11 | + temp_dict = {}
|
|---|
| 12 | opts = model._meta
|
|---|
| 13 | for f in opts.fields + opts.many_to_many:
|
|---|
| 14 | if not f.editable:
|
|---|
| 15 | @@ -146,7 +146,11 @@
|
|---|
| 16 | continue
|
|---|
| 17 | formfield = formfield_callback(f)
|
|---|
| 18 | if formfield:
|
|---|
| 19 | + temp_dict[f.name] = formfield
|
|---|
| 20 | field_list.append((f.name, formfield))
|
|---|
| 21 | + if fields:
|
|---|
| 22 | + # reorder to match ordering in 'fields' if it was provided
|
|---|
| 23 | + field_list = [(fn,temp_dict.get(fn)) for fn in fields if temp_dict.has_key(fn)]
|
|---|
| 24 | return SortedDict(field_list)
|
|---|
| 25 |
|
|---|
| 26 | class ModelFormOptions(object):
|
|---|
| 27 | @@ -219,9 +223,9 @@
|
|---|
| 28 | fields_on_form = [field for field in check if field in self.fields]
|
|---|
| 29 | if len(fields_on_form) == len(check):
|
|---|
| 30 | unique_checks.append(check)
|
|---|
| 31 | -
|
|---|
| 32 | +
|
|---|
| 33 | form_errors = []
|
|---|
| 34 | -
|
|---|
| 35 | +
|
|---|
| 36 | # Gather a list of checks for fields declared as unique and add them to
|
|---|
| 37 | # the list of checks. Again, skip fields not on the form.
|
|---|
| 38 | for name, field in self.fields.items():
|
|---|
| 39 | @@ -235,30 +239,30 @@
|
|---|
| 40 | is_null_pk = f.primary_key and self.cleaned_data[name] is None
|
|---|
| 41 | if name in self.cleaned_data and f.unique and not is_null_pk:
|
|---|
| 42 | unique_checks.append((name,))
|
|---|
| 43 | -
|
|---|
| 44 | +
|
|---|
| 45 | # Don't run unique checks on fields that already have an error.
|
|---|
| 46 | unique_checks = [check for check in unique_checks if not [x in self._errors for x in check if x in self._errors]]
|
|---|
| 47 | -
|
|---|
| 48 | +
|
|---|
| 49 | for unique_check in unique_checks:
|
|---|
| 50 | # Try to look up an existing object with the same values as this
|
|---|
| 51 | # object's values for all the unique field.
|
|---|
| 52 | -
|
|---|
| 53 | +
|
|---|
| 54 | lookup_kwargs = {}
|
|---|
| 55 | for field_name in unique_check:
|
|---|
| 56 | lookup_kwargs[field_name] = self.cleaned_data[field_name]
|
|---|
| 57 | -
|
|---|
| 58 | +
|
|---|
| 59 | qs = self.instance.__class__._default_manager.filter(**lookup_kwargs)
|
|---|
| 60 |
|
|---|
| 61 | - # Exclude the current object from the query if we are editing an
|
|---|
| 62 | + # Exclude the current object from the query if we are editing an
|
|---|
| 63 | # instance (as opposed to creating a new one)
|
|---|
| 64 | if self.instance.pk is not None:
|
|---|
| 65 | qs = qs.exclude(pk=self.instance.pk)
|
|---|
| 66 | -
|
|---|
| 67 | +
|
|---|
| 68 | # This cute trick with extra/values is the most efficient way to
|
|---|
| 69 | # tell if a particular query returns any results.
|
|---|
| 70 | if qs.extra(select={'a': 1}).values('a').order_by():
|
|---|
| 71 | model_name = capfirst(self.instance._meta.verbose_name)
|
|---|
| 72 | -
|
|---|
| 73 | +
|
|---|
| 74 | # A unique field
|
|---|
| 75 | if len(unique_check) == 1:
|
|---|
| 76 | field_name = unique_check[0]
|
|---|
| 77 | @@ -278,11 +282,11 @@
|
|---|
| 78 | {'model_name': unicode(model_name),
|
|---|
| 79 | 'field_label': unicode(field_labels)}
|
|---|
| 80 | )
|
|---|
| 81 | -
|
|---|
| 82 | +
|
|---|
| 83 | # Remove the data from the cleaned_data dict since it was invalid
|
|---|
| 84 | for field_name in unique_check:
|
|---|
| 85 | del self.cleaned_data[field_name]
|
|---|
| 86 | -
|
|---|
| 87 | +
|
|---|
| 88 | if form_errors:
|
|---|
| 89 | # Raise the unique together errors since they are considered form-wide.
|
|---|
| 90 | raise ValidationError(form_errors)
|
|---|
| 91 | @@ -471,7 +475,7 @@
|
|---|
| 92 | kwargs = {self.fk.get_attname(): self.instance.pk}
|
|---|
| 93 | new_obj = self.model(**kwargs)
|
|---|
| 94 | return save_instance(form, new_obj, exclude=[self._pk_field.name], commit=commit)
|
|---|
| 95 | -
|
|---|
| 96 | +
|
|---|
| 97 | def add_fields(self, form, index):
|
|---|
| 98 | super(BaseInlineFormSet, self).add_fields(form, index)
|
|---|
| 99 | if self._pk_field == self.fk:
|
|---|