Added creation_counter to BaseField in order to provied form modules with a way to sort fields i order user specified them (same technique is used in Django)

This commit is contained in:
Viktor Kerkez 2010-10-23 22:35:37 +02:00
parent f0c5dd1bce
commit ef15733efe

View File

@ -25,6 +25,12 @@ class BaseField(object):
_index_with_types = True
_geo_index = False
# These track each time a Field instance is created. Used to retain order.
# The auto_creation_counter is used for fields that MongoEngine implicitly
# creates, creation_counter is used for all user-specified fields.
creation_counter = 0
auto_creation_counter = -1
def __init__(self, db_field=None, name=None, required=False, default=None,
unique=False, unique_with=None, primary_key=False,
validation=None, choices=None):
@ -41,6 +47,13 @@ class BaseField(object):
self.primary_key = primary_key
self.validation = validation
self.choices = choices
# Adjust the appropriate creation counter, and save our local copy.
if self.db_field == '_id':
self.creation_counter = BaseField.auto_creation_counter
BaseField.auto_creation_counter -= 1
else:
self.creation_counter = BaseField.creation_counter
BaseField.creation_counter += 1
def __get__(self, instance, owner):
"""Descriptor for retrieving a value from a field in a document. Do