17
17
from mongoengine .base import ValidationError
18
18
from mongoengine .connection import _get_db
19
19
20
- from util import MongoFormFieldGenerator
21
- from documentoptions import AdminOptions
20
+ from fieldgenerator import MongoDefaultFormFieldGenerator
21
+ from documentoptions import DocumentMetaWrapper
22
22
23
23
24
24
def _get_unique_filename (name ):
@@ -134,7 +134,7 @@ def document_to_dict(instance, fields=None, exclude=None):
134
134
return data
135
135
136
136
def fields_for_document (document , fields = None , exclude = None , widgets = None , \
137
- formfield_callback = None , field_generator = MongoFormFieldGenerator ):
137
+ formfield_callback = None , field_generator = MongoDefaultFormFieldGenerator ):
138
138
"""
139
139
Returns a ``SortedDict`` containing form fields for the given model.
140
140
@@ -195,13 +195,15 @@ class ModelFormOptions(object):
195
195
def __init__ (self , options = None ):
196
196
self .document = getattr (options , 'document' , None )
197
197
self .model = self .document
198
- if self .model is not None and isinstance (self .model ._meta , dict ):
199
- self .model ._admin_opts = AdminOptions (self .model )
200
- self .model ._meta = self .model ._admin_opts
198
+ # set up the document meta wrapper if document meta is a dict
199
+ if self .document is not None and isinstance (self .document ._meta , dict ):
200
+ self .document ._admin_opts = DocumentMetaWrapper (self .document )
201
+ self .document ._meta = self .document ._admin_opts
201
202
self .fields = getattr (options , 'fields' , None )
202
203
self .exclude = getattr (options , 'exclude' , None )
203
204
self .widgets = getattr (options , 'widgets' , None )
204
205
self .embedded_field = getattr (options , 'embedded_field_name' , None )
206
+ self .formfield_generator = getattr (options , 'formfield_generator' , None )
205
207
206
208
207
209
class DocumentFormMetaclass (type ):
@@ -222,7 +224,7 @@ def __new__(cls, name, bases, attrs):
222
224
223
225
opts = new_class ._meta = ModelFormOptions (getattr (new_class , 'Meta' , None ))
224
226
if opts .document :
225
- formfield_generator = getattr (opts , 'formfield_generator' , MongoFormFieldGenerator )
227
+ formfield_generator = getattr (opts , 'formfield_generator' , MongoDefaultFormFieldGenerator )
226
228
227
229
# If a model is defined, extract form fields from it.
228
230
fields = fields_for_document (opts .document , opts .fields ,
@@ -474,6 +476,7 @@ def save(self, commit=True):
474
476
raise ValueError ("The %s could not be saved because the data didn't"
475
477
" validate." % self .instance .__class__ .__name__ )
476
478
479
+
477
480
if commit :
478
481
l = getattr (self .parent_document , self ._meta .embedded_field )
479
482
l .append (self .instance )
@@ -513,24 +516,6 @@ def initial_form_count(self):
513
516
return len (self .get_queryset ())
514
517
return super (BaseDocumentFormSet , self ).initial_form_count ()
515
518
516
- def _construct_form (self , i , ** kwargs ):
517
- #if self.is_bound and i < self.initial_form_count():
518
- # Import goes here instead of module-level because importing
519
- # django.db has side effects.
520
- #from django.db import connections
521
- # pk_key = "%s-%s" % (self.add_prefix(i), self.model._meta.pk.name)
522
- # pk = self.data[pk_key]
523
- # pk_field = self.model._meta.pk
524
- # pk = pk_field.get_db_prep_lookup('exact', pk,
525
- # connection=connections[self.get_queryset().db])
526
- # if isinstance(pk, list):
527
- # pk = pk[0]
528
- # kwargs['instance'] = self._existing_object(pk)
529
- #if i < self.initial_form_count() and not kwargs.get('instance'):
530
- # kwargs['instance'] = self.get_queryset()[i]
531
- form = super (BaseDocumentFormSet , self )._construct_form (i , ** kwargs )
532
- return form
533
-
534
519
def get_queryset (self ):
535
520
return self ._queryset
536
521
@@ -582,37 +567,6 @@ def get_date_error_message(self, date_check):
582
567
def get_form_error (self ):
583
568
return ugettext ("Please correct the duplicate values below." )
584
569
585
- def add_fields (self , form , index ):
586
- # """Add a hidden field for the object's primary key."""
587
- # from django.db.models import AutoField, OneToOneField, ForeignKey
588
- # self._pk_field = pk = self.model._meta.pk
589
- # # If a pk isn't editable, then it won't be on the form, so we need to
590
- # # add it here so we can tell which object is which when we get the
591
- # # data back. Generally, pk.editable should be false, but for some
592
- # # reason, auto_created pk fields and AutoField's editable attribute is
593
- # # True, so check for that as well.
594
- # def pk_is_not_editable(pk):
595
- # return ((not pk.editable) or (pk.auto_created or isinstance(pk, AutoField))
596
- # or (pk.rel and pk.rel.parent_link and pk_is_not_editable(pk.rel.to._meta.pk)))
597
- # if pk_is_not_editable(pk) or pk.name not in form.fields:
598
- # if form.is_bound:
599
- # pk_value = form.instance.pk
600
- # else:
601
- # try:
602
- # if index is not None:
603
- # pk_value = self.get_queryset()[index].pk
604
- # else:
605
- # pk_value = None
606
- # except IndexError:
607
- # pk_value = None
608
- # if isinstance(pk, OneToOneField) or isinstance(pk, ForeignKey):
609
- # qs = pk.rel.to._default_manager.get_query_set()
610
- # else:
611
- # qs = self.model._default_manager.get_query_set()
612
- # qs = qs.using(form.instance._state.db)
613
- # #form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=HiddenInput)
614
- super (BaseDocumentFormSet , self ).add_fields (form , index )
615
-
616
570
def documentformset_factory (document , form = DocumentForm , formfield_callback = None ,
617
571
formset = BaseDocumentFormSet ,
618
572
extra = 1 , can_delete = False , can_order = False ,
0 commit comments