Fix Create User form's fields sorting for Django 1.7
Starting from Django 1.7 self.fields.keyOrder no longer works for setting fields ordering, need to rearrange fields there by recreating underlying OrderedDict. Change-Id: If0f4c7014a7a68f2ff0d0f2cb06f7b1c2e28436b Closes-Bug: #1465221
This commit is contained in:
@@ -16,8 +16,10 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
|
import collections
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
import django
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.forms import ValidationError # noqa
|
from django.forms import ValidationError # noqa
|
||||||
from django import http
|
from django import http
|
||||||
@@ -106,9 +108,15 @@ class CreateUserForm(PasswordMixin, BaseUserForm):
|
|||||||
roles = kwargs.pop('roles')
|
roles = kwargs.pop('roles')
|
||||||
super(CreateUserForm, self).__init__(*args, **kwargs)
|
super(CreateUserForm, self).__init__(*args, **kwargs)
|
||||||
# Reorder form fields from multiple inheritance
|
# Reorder form fields from multiple inheritance
|
||||||
self.fields.keyOrder = ["domain_id", "domain_name", "name",
|
ordering = ["domain_id", "domain_name", "name", "email", "password",
|
||||||
"email", "password", "confirm_password",
|
"confirm_password", "project", "role_id"]
|
||||||
"project", "role_id"]
|
# Starting from 1.7 Django uses OrderedDict for fields and keyOrder
|
||||||
|
# no longer works for it
|
||||||
|
if django.VERSION >= (1, 7):
|
||||||
|
self.fields = collections.OrderedDict(
|
||||||
|
(key, self.fields[key]) for key in ordering)
|
||||||
|
else:
|
||||||
|
self.fields.keyOrder = ordering
|
||||||
role_choices = [(role.id, role.name) for role in roles]
|
role_choices = [(role.id, role.name) for role in roles]
|
||||||
self.fields['role_id'].choices = role_choices
|
self.fields['role_id'].choices = role_choices
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user