示例#1
0
                                FileField, MapField, EmbeddedDocumentField)
try:
    from mongoengine.base import ValidationError
except ImportError:
    from mongoengine.errors import ValidationError
from mongoengine.queryset import OperationError, Q
from mongoengine.queryset.base import BaseQuerySet
from mongoengine.connection import get_db, DEFAULT_CONNECTION_NAME
from mongoengine.base import NON_FIELD_ERRORS as MONGO_NON_FIELD_ERRORS

from gridfs import GridFS

from mongodbforms.documentoptions import DocumentMetaWrapper
from mongodbforms.util import with_metaclass, load_field_generator

_fieldgenerator = load_field_generator()


def _get_unique_filename(name, db_alias=DEFAULT_CONNECTION_NAME,
                         collection_name='fs'):
    fs = GridFS(get_db(db_alias), collection_name)
    file_root, file_ext = os.path.splitext(get_valid_filename(name))
    count = itertools.count(1)
    while fs.exists(filename=name):
        # file_ext includes the dot.
        name = os.path.join("%s_%s%s" % (file_root, next(count), file_ext))
    return name


def _save_iterator_file(field, instance, uploaded_file, file_data=None):
    """
示例#2
0
from mongoadmin.widgets import ReferenceRawIdWidget, MultiReferenceRawIdWidget

# Defaults for formfield_overrides. ModelAdmin subclasses can change this
# by adding to ModelAdmin.formfield_overrides.
FORMFIELD_FOR_DBFIELD_DEFAULTS = {
    DateTimeField: {
        'form_class': forms.SplitDateTimeField,
        'widget': widgets.AdminSplitDateTime
    },
    URLField:       {'widget': widgets.AdminURLFieldWidget},
    IntField:       {'widget': widgets.AdminIntegerFieldWidget},
    ImageField:     {'widget': widgets.AdminFileWidget},
    FileField:      {'widget': widgets.AdminFileWidget},
}

_fieldgenerator = load_field_generator()()


def formfield(field, form_class=None, **kwargs):
    """
    Returns a django.forms.Field instance for this database Field.
    """
    defaults = {'required': field.required}
    if field.default is not None:
        if isinstance(field.default, collections.Callable):
            defaults['initial'] = field.default()
            defaults['show_hidden_initial'] = True
        else:
            defaults['initial'] = field.default

    if field.choices is not None: