def get_form(self, request, obj=None, **kwargs):
        """
        Returns a Form class for use in the admin add view. This is used by
        add_view and change_view.
        """
        if 'fields' in kwargs:
            fields = kwargs.pop('fields')
        else:
            fields = flatten_fieldsets(self.get_fieldsets(request, obj))
        if self.exclude is None:
            exclude = []
        else:
            exclude = list(self.exclude)
        exclude.extend(self.get_readonly_fields(request, obj))
        if self.exclude is None and hasattr(
                self.form, '_meta') and self.form._meta.exclude:
            # Take the custom ModelForm's Meta.exclude into account only if the
            # ModelAdmin doesn't define its own.
            exclude.extend(self.form._meta.exclude)
        # if exclude is an empty list we pass None to be consistent with the
        # default on modelform_factory
        exclude = exclude or None
        defaults = {
            "form":
            self.form,
            "fields":
            fields,
            "exclude":
            exclude,
            "formfield_callback":
            partial(self.formfield_for_dbfield, request=request),
        }
        defaults.update(kwargs)

        if defaults['fields'] is None and not modelform_defines_fields(
                defaults['form']):
            defaults['fields'] = None

        print(defaults)
        try:
            return documentform_factory(self.model, **defaults)
        except FieldError as e:
            print(e.message)
            raise FieldError(
                '%s. Check fields/fieldsets/exclude attributes of class %s.' %
                (e, self.__class__.__name__))
Example #2
0
 def get_form_class(self):
     """
     Returns the form class to use in this view
     """
     if self.form_class:
         return self.form_class
     else:
         if self.document is not None:
             # If a document has been explicitly provided, use it
             document = self.document
         elif hasattr(self, 'object') and self.object is not None:
             # If this view is operating on a single object, use
             # the class of that object
             document = self.object.__class__
         else:
             # Try to get a queryset and extract the document class
             # from that
             document = self.get_queryset()._document
         return documentform_factory(document)
Example #3
0
 def get_form_class(self):
     """
     Returns the form class to use in this view
     """
     if self.form_class:
         return self.form_class
     else:
         if self.document is not None:
             # If a document has been explicitly provided, use it
             document = self.document
         elif hasattr(self, 'object') and self.object is not None:
             # If this view is operating on a single object, use
             # the class of that object
             document = self.object.__class__
         else:
             # Try to get a queryset and extract the document class
             # from that
             document = self.get_queryset()._document
         return documentform_factory(document)
Example #4
0
    def get_form(self, request, obj=None, **kwargs):
        """
        Returns a Form class for use in the admin add view. This is used by
        add_view and change_view.
        """
        if 'fields' in kwargs:
            fields = kwargs.pop('fields')
        else:
            fields = flatten_fieldsets(self.get_fieldsets(request, obj))
        if self.exclude is None:
            exclude = []
        else:
            exclude = list(self.exclude)
        exclude.extend(self.get_readonly_fields(request, obj))
        if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude:
            # Take the custom ModelForm's Meta.exclude into account only if the
            # ModelAdmin doesn't define its own.
            exclude.extend(self.form._meta.exclude)
        # if exclude is an empty list we pass None to be consistent with the
        # default on modelform_factory
        exclude = exclude or None
        defaults = {
            "form": self.form,
            "fields": fields,
            "exclude": exclude,
            "formfield_callback": partial(self.formfield_for_dbfield, request=request),
        }
        defaults.update(kwargs)

        if defaults['fields'] is None and not modelform_defines_fields(defaults['form']):
            defaults['fields'] = None

        print(defaults)
        try:
            return documentform_factory(self.model, **defaults)
        except FieldError as e:
            print(e.message)
            raise FieldError('%s. Check fields/fieldsets/exclude attributes of class %s.'
                             % (e, self.__class__.__name__))