Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed #5986 -- Added ability to customize order of Form fields
- Loading branch information
Showing
5 changed files
with
100 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,9 +73,11 @@ class BaseForm(object): | |
# class is different than Form. See the comments by the Form class for more | ||
# information. Any improvements to the form API should be made to *this* | ||
# class, not to the Form class. | ||
field_order = None | ||
|
||
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, | ||
initial=None, error_class=ErrorList, label_suffix=None, | ||
empty_permitted=False): | ||
empty_permitted=False, field_order=None): | ||
self.is_bound = data is not None or files is not None | ||
self.data = data or {} | ||
self.files = files or {} | ||
|
@@ -96,6 +98,29 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, | |
# self.base_fields. | ||
self.fields = copy.deepcopy(self.base_fields) | ||
self._bound_fields_cache = {} | ||
self.order_fields(self.field_order if field_order is None else field_order) | ||
|
||
def order_fields(self, field_order): | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
ttanner
Author
Contributor
|
||
""" | ||
Rearranges the fields according to field_order. | ||
field_order is a list of field names specifying the order. Fields not | ||
included in the list are appended in the default order for backward | ||
compatibility with subclasses not overriding field_order. If field_order | ||
is None, all fields are kept in the order defined in the class. | ||
Unknown fields in field_order are ignored to allow disabling fields in | ||
form subclasses without redefining ordering. | ||
""" | ||
if field_order is None: | ||
return | ||
fields = OrderedDict() | ||
for key in field_order: | ||
try: | ||
fields[key] = self.fields.pop(key) | ||
except KeyError: # ignore unknown fields | ||
pass | ||
fields.update(self.fields) # add remaining fields in original order | ||
self.fields = fields | ||
|
||
def __str__(self): | ||
return self.as_table() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@timgraham, @ttanner What about a
ordered_fields
property instead? Then you'd haveor similar. Just came to my mind, since we don't really have a pattern like that in Django anywhere, the admin usually has getter methods for everything, so just from a naming perspective I'd at least name it
get_ordered_fields
and return the fields…