Index: models.py =================================================================== --- models.py (revision 8961) +++ models.py (working copy) @@ -134,8 +134,8 @@ fields will be excluded from the returned fields, even if they are listed in the ``fields`` argument. """ - # TODO: if fields is provided, it would be nice to return fields in that order field_list = [] + temp_dict = {} opts = model._meta for f in opts.fields + opts.many_to_many: if not f.editable: @@ -146,7 +146,11 @@ continue formfield = formfield_callback(f) if formfield: + temp_dict[f.name] = formfield field_list.append((f.name, formfield)) + if fields: + # reorder to match ordering in 'fields' if it was provided + field_list = [(fn,temp_dict.get(fn)) for fn in fields if temp_dict.has_key(fn)] return SortedDict(field_list) class ModelFormOptions(object): @@ -219,9 +223,9 @@ fields_on_form = [field for field in check if field in self.fields] if len(fields_on_form) == len(check): unique_checks.append(check) - + form_errors = [] - + # Gather a list of checks for fields declared as unique and add them to # the list of checks. Again, skip fields not on the form. for name, field in self.fields.items(): @@ -235,30 +239,30 @@ is_null_pk = f.primary_key and self.cleaned_data[name] is None if name in self.cleaned_data and f.unique and not is_null_pk: unique_checks.append((name,)) - + # Don't run unique checks on fields that already have an error. unique_checks = [check for check in unique_checks if not [x in self._errors for x in check if x in self._errors]] - + for unique_check in unique_checks: # Try to look up an existing object with the same values as this # object's values for all the unique field. - + lookup_kwargs = {} for field_name in unique_check: lookup_kwargs[field_name] = self.cleaned_data[field_name] - + qs = self.instance.__class__._default_manager.filter(**lookup_kwargs) - # Exclude the current object from the query if we are editing an + # Exclude the current object from the query if we are editing an # instance (as opposed to creating a new one) if self.instance.pk is not None: qs = qs.exclude(pk=self.instance.pk) - + # This cute trick with extra/values is the most efficient way to # tell if a particular query returns any results. if qs.extra(select={'a': 1}).values('a').order_by(): model_name = capfirst(self.instance._meta.verbose_name) - + # A unique field if len(unique_check) == 1: field_name = unique_check[0] @@ -278,11 +282,11 @@ {'model_name': unicode(model_name), 'field_label': unicode(field_labels)} ) - + # Remove the data from the cleaned_data dict since it was invalid for field_name in unique_check: del self.cleaned_data[field_name] - + if form_errors: # Raise the unique together errors since they are considered form-wide. raise ValidationError(form_errors) @@ -471,7 +475,7 @@ kwargs = {self.fk.get_attname(): self.instance.pk} new_obj = self.model(**kwargs) return save_instance(form, new_obj, exclude=[self._pk_field.name], commit=commit) - + def add_fields(self, form, index): super(BaseInlineFormSet, self).add_fields(form, index) if self._pk_field == self.fk: