Index: django/newforms/forms.py
===================================================================
--- django/newforms/forms.py	(wersja 6706)
+++ django/newforms/forms.py	(kopia robocza)
@@ -8,6 +8,7 @@
 from django.utils.html import escape
 from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
 from django.utils.safestring import mark_safe
+from django.core.management.base import CommandError
 
 from fields import Field
 from widgets import TextInput, Textarea
@@ -38,6 +39,14 @@
             if hasattr(base, 'base_fields'):
                 fields = base.base_fields.items() + fields
 
+        # Sort by Meta.fields_order if present.
+        # Raises CommandError if any of the fields is missing.
+        if attrs.has_key('Meta') and hasattr(attrs['Meta'], 'fields_order'):
+            for f in fields:
+                if not f[0] in attrs['Meta'].fields_order:
+                    raise CommandError, "%s not present in %s.Meta.fields_order" % (f[0], name)
+            fields.sort(lambda x, y: cmp(attrs['Meta'].fields_order.index(x[0]), attrs['Meta'].fields_order.index(y[0])))
+
         attrs['base_fields'] = SortedDict(fields)
         return type.__new__(cls, name, bases, attrs)
 
Index: tests/regressiontests/forms/fields_order.py
===================================================================
--- tests/regressiontests/forms/fields_order.py	(wersja 0)
+++ tests/regressiontests/forms/fields_order.py	(wersja 0)
@@ -0,0 +1,72 @@
+# -*- coding: utf-8 -*-
+
+tests = r"""
+>>> from django.newforms import *
+>>> class TestForm(Form):
+...     f1=CharField(max_length=10)
+... 
+>>> TestForm(auto_id=False).as_p()
+u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
+>>> class TestForm(Form):
+...     f1=CharField(max_length=10)
+...     class Meta:
+...         fields_order=['baz']
+... 
+Traceback (most recent call last):
+...
+CommandError: f1 not present in TestForm.Meta.fields_order
+>>> class TestForm(Form):
+...     f1=CharField(max_length=10)
+...     f2=CharField(max_length=10)
+...     class Meta:
+...         fields_order=['f1']
+... 
+Traceback (most recent call last):
+...
+CommandError: f2 not present in TestForm.Meta.fields_order
+>>> class TestForm(Form):
+...     f1=CharField(max_length=10)
+...     class Meta:
+...         fields_order=['f1']
+... 
+>>> TestForm(auto_id=False).as_p()
+u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
+>>> class TestForm(Form):
+...     f1=CharField(max_length=10)
+...     f2=CharField(max_length=10)
+...     class Meta:
+...         fields_order=['f1','f2']
+... 
+>>> TestForm(auto_id=False).as_p()
+u'<p>F1: <input type="text" name="f1" maxlength="10" /></p>\n<p>F2: <input type="text" name="f2" maxlength="10" /></p>'
+>>> class TestForm(Form):
+...     f1=CharField(max_length=10)
+...     f2=CharField(max_length=10)
+...     class Meta:
+...         fields_order=['f2','f1']
+... 
+>>> TestForm(auto_id=False).as_p()
+u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
+>>> class SomeForm(TestForm):
+...     f3=CharField(max_length=10)
+... 
+>>> TestForm(auto_id=False).as_p()
+u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
+>>> SomeForm(auto_id=False).as_p()
+u'<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>\n<p>F3: <input type="text" name="f3" maxlength="10" /></p>'
+>>> class SomeForm(TestForm):
+...     f3=CharField(max_length=10)
+...     class Meta:
+...         fields_order=['f3']
+... 
+Traceback (most recent call last):
+...
+CommandError: f2 not present in SomeForm.Meta.fields_order
+>>> class SomeForm(TestForm):
+...     f3=CharField(max_length=10)
+...     class Meta:
+...         fields_order=['f3','f2','f1']
+... 
+>>> SomeForm(auto_id=False).as_p()
+u'<p>F3: <input type="text" name="f3" maxlength="10" /></p>\n<p>F2: <input type="text" name="f2" maxlength="10" /></p>\n<p>F1: <input type="text" name="f1" maxlength="10" /></p>'
+"""
Index: tests/regressiontests/forms/tests.py
===================================================================
--- tests/regressiontests/forms/tests.py	(wersja 6706)
+++ tests/regressiontests/forms/tests.py	(kopia robocza)
@@ -2,6 +2,7 @@
 from extra import tests as extra_tests
 from fields import tests as fields_tests
 from forms import tests as form_tests
+from fields_order import tests as fields_order_tests
 from error_messages import tests as custom_error_message_tests
 from localflavor.ar import tests as localflavor_ar_tests
 from localflavor.au import tests as localflavor_au_tests
@@ -29,6 +30,7 @@
 __test__ = {
     'extra_tests': extra_tests,
     'fields_tests': fields_tests,
+    'fields_order_test': fields_order_tests,
     'form_tests': form_tests,
     'custom_error_message_tests': custom_error_message_tests,
     'localflavor_ar_tests': localflavor_ar_tests,
