@@ -444,7 +444,11 @@ def documentform_factory(document, form=DocumentForm, fields=None, exclude=None,
444
444
Meta = type ('Meta' , parent , attrs )
445
445
446
446
# Give this new form class a reasonable name.
447
- class_name = document .__class__ .__name__ + 'Form'
447
+ if isinstance (document , type ):
448
+ doc_inst = document ()
449
+ else :
450
+ doc_inst = document
451
+ class_name = doc_inst .__class__ .__name__ + 'Form'
448
452
449
453
# Class attributes for the new form class.
450
454
form_class_attrs = {
@@ -609,62 +613,39 @@ def add_fields(self, form, index):
609
613
# #form.fields[self._pk_field.name] = ModelChoiceField(qs, initial=pk_value, required=False, widget=HiddenInput)
610
614
super (BaseDocumentFormSet , self ).add_fields (form , index )
611
615
612
- def documentformset_factory (model , form = DocumentForm , formfield_callback = None ,
616
+ def documentformset_factory (document , form = DocumentForm , formfield_callback = None ,
613
617
formset = BaseDocumentFormSet ,
614
618
extra = 1 , can_delete = False , can_order = False ,
615
619
max_num = None , fields = None , exclude = None ):
616
620
"""
617
621
Returns a FormSet class for the given Django model class.
618
622
"""
619
- form = documentform_factory (model , form = form , fields = fields , exclude = exclude ,
623
+ form = documentform_factory (document , form = form , fields = fields , exclude = exclude ,
620
624
formfield_callback = formfield_callback )
621
625
FormSet = formset_factory (form , formset , extra = extra , max_num = max_num ,
622
626
can_order = can_order , can_delete = can_delete )
623
- FormSet .model = model
627
+ FormSet .model = document
628
+ FormSet .document = document
624
629
return FormSet
625
630
626
-
627
-
628
631
class BaseInlineDocumentFormSet (BaseDocumentFormSet ):
629
632
"""
630
633
A formset for child objects related to a parent.
631
634
632
635
self.instance -> the document containing the inline objects
633
636
"""
634
637
def __init__ (self , data = None , files = None , instance = None ,
635
- save_as_new = False , prefix = None , queryset = None ):
638
+ save_as_new = False , prefix = None , queryset = [], ** kwargs ):
636
639
self .instance = instance
637
640
self .save_as_new = save_as_new
638
641
639
- if queryset is None :
640
- queryset = self .document ._default_manager
641
-
642
- try :
643
- qs = queryset .filter (** {self .fk .name : self .instance })
644
- except AttributeError :
645
- # we received a list (hopefully)
646
- print "FIXME: a real queryset would be nice"
647
- qs = queryset
648
- super (BaseInlineDocumentFormSet , self ).__init__ (data , files , prefix = prefix , queryset = qs )
642
+ super (BaseInlineDocumentFormSet , self ).__init__ (data , files , prefix = prefix , queryset = queryset , ** kwargs )
649
643
650
644
def initial_form_count (self ):
651
645
if self .save_as_new :
652
646
return 0
653
647
return super (BaseInlineDocumentFormSet , self ).initial_form_count ()
654
648
655
-
656
- def _construct_form (self , i , ** kwargs ):
657
- form = super (BaseInlineDocumentFormSet , self )._construct_form (i , ** kwargs )
658
- if self .save_as_new :
659
- # Remove the primary key from the form's data, we are only
660
- # creating new instances
661
- form .data [form .add_prefix (self ._pk_field .name )] = None
662
-
663
- # Remove the foreign key from the form's data
664
- form .data [form .add_prefix (self .fk .name )] = None
665
-
666
- return form
667
-
668
649
#@classmethod
669
650
def get_default_prefix (cls ):
670
651
return cls .model .__name__ .lower ()
@@ -686,8 +667,8 @@ def get_unique_error_message(self, unique_check):
686
667
return super (BaseInlineDocumentFormSet , self ).get_unique_error_message (unique_check )
687
668
688
669
689
- def inlineformset_factory (parent_document , document , form = DocumentForm ,
690
- formset = BaseInlineDocumentFormSet , fk_name = None ,
670
+ def inlineformset_factory (document , form = DocumentForm ,
671
+ formset = BaseInlineDocumentFormSet ,
691
672
fields = None , exclude = None ,
692
673
extra = 1 , can_order = False , can_delete = True , max_num = None ,
693
674
formfield_callback = None ):
@@ -711,3 +692,40 @@ def inlineformset_factory(parent_document, document, form=DocumentForm,
711
692
FormSet = documentformset_factory (document , ** kwargs )
712
693
return FormSet
713
694
695
+ class EmbeddedDocumentFormSet (BaseInlineDocumentFormSet ):
696
+ def __init__ (self , parent_document = None , data = None , files = None , instance = None ,
697
+ save_as_new = False , prefix = None , queryset = [], ** kwargs ):
698
+ self .parent_document = parent_document
699
+ super (EmbeddedDocumentFormSet , self ).__init__ (data , files , instance , save_as_new , prefix , queryset , ** kwargs )
700
+
701
+ def _construct_form (self , i , ** kwargs ):
702
+ defaults = {'parent_document' : self .parent_document }
703
+ defaults .update (kwargs )
704
+ form = super (BaseDocumentFormSet , self )._construct_form (i , ** defaults )
705
+ return form
706
+
707
+ def embeddedformset_factory (document , parent_document , form = EmbeddedDocumentForm ,
708
+ formset = EmbeddedDocumentFormSet ,
709
+ fields = None , exclude = None ,
710
+ extra = 1 , can_order = False , can_delete = True , max_num = None ,
711
+ formfield_callback = None ):
712
+ """
713
+ Returns an ``InlineFormSet`` for the given kwargs.
714
+
715
+ You must provide ``fk_name`` if ``model`` has more than one ``ForeignKey``
716
+ to ``parent_model``.
717
+ """
718
+ kwargs = {
719
+ 'form' : form ,
720
+ 'formfield_callback' : formfield_callback ,
721
+ 'formset' : formset ,
722
+ 'extra' : extra ,
723
+ 'can_delete' : can_delete ,
724
+ 'can_order' : can_order ,
725
+ 'fields' : fields ,
726
+ 'exclude' : exclude ,
727
+ 'max_num' : max_num ,
728
+ }
729
+ FormSet = inlineformset_factory (document , ** kwargs )
730
+ FormSet .parent_document = parent_document
731
+ return FormSet
0 commit comments