Tuesday, July 31, 2012

Working with a Custom, Dynamic Form (using BaseForm)

Hi,

Have hit a huge roadblock trying to implement a custom, dynamic form into my django project. Although I am more familiar with using ModelForms, I need to use BaseForm to work with the structure I already have in place.

I have a dictionary of objects inside of my Model 'Photographer' that I am trying to pass into my form. The objects from 'Photographer' will be a drop down option for the user to select from, then will become part of an instance of another model 'A' after the user submits the form. I cannot figure out how to include the objects from 'Photographer' into my form, given the following form structure:

    class AForm(BaseForm):
        def __init__(self, data=None, files=None, instance=None, auto_id='id_%s',
                     prefix=None, initial=None, error_class=ErrorList,
                     label_suffix=':', empty_permitted=False):

            if not instance:
                raise NotImplementedError("Instance must be provided")

            self.instance = instance
            object_data = self.instance.fields_dict()
            self.declared_fields = SortedDict()
            self.base_fields = fields_for_a(self.instance)

            # if initial was provided, it should override the values from instance
            if initial is not None:
                object_data.update(initial)

            BaseForm.__init__(self, data, files, auto_id, prefix, object_data,
                              error_class, label_suffix, empty_permitted)

            cleaned_data = self.cleaned_data

            # save fieldvalues for self.instance
            fields = field_list(self.instance)

            for field in fields:
                if field.enable_wysiwyg:
                    value = unicode(strip(cleaned_data[field.name]))
                else:
                    value = unicode(cleaned_data[field.name])

            return self.instance

For a more full representation of the problem in general, including all of the models and a lot of the view code, please see my stack overflow question: http://stackoverflow.com/questions/11548992/adding-values-to-complex-django-view. 

I cannot thank you enough for any advice given. I have been struggling with this issue for over a month, sadly. I appreciate any commentary.


Thank you!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/GkiNhsL225kJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment