Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Working with a Custom, Dynamic Form (using BaseForm)
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Nick_B  
View profile  
 More options Jul 31 2012, 1:14 pm
From: Nick_B <nickbew...@gmail.com>
Date: Tue, 31 Jul 2012 10:14:17 -0700 (PDT)
Local: Tues, Jul 31 2012 1:14 pm
Subject: 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-....

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Anton Baklanov  
View profile  
 More options Jul 31 2012, 3:05 pm
From: Anton Baklanov <antonbakla...@gmail.com>
Date: Tue, 31 Jul 2012 22:05:41 +0300
Local: Tues, Jul 31 2012 3:05 pm
Subject: Re: Working with a Custom, Dynamic Form (using BaseForm)

what is going on here:
    self.base_fields = fields_for_a(self.instance) ?

--
Regards,
Anton Baklanov

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick_B  
View profile  
 More options Aug 1 2012, 1:36 pm
From: Nick_B <nickbew...@gmail.com>
Date: Wed, 1 Aug 2012 10:36:45 -0700 (PDT)
Local: Wed, Aug 1 2012 1:36 pm
Subject: Re: Working with a Custom, Dynamic Form (using BaseForm)

Hi Anton,

Thank you very much for your response. The 'fields_for_a' is generating the
form fields for the instance of the model 'A'. The author admits that it
might not be the prettiest implementation, but fully functional.

def fields_for_a(instance):
    # generate a sorted dict of fields corresponding to the Field model
    # for the A instance
    fields_dict = SortedDict()
    fields = field_list(instance)
    # this really, really should be refactored
    for field in fields:
        if field.field_type == Field.BOOLEAN_FIELD:
            fields_dict[field.name] = forms.BooleanField(label=field.label,
required=False, help_text=field.help_text)
        elif field.field_type == Field.CHAR_FIELD:
            widget = forms.TextInput
            fields_dict[field.name] = forms.CharField(label=field.label,
required=field.required, max_length=field.max_length,
help_text=field.help_text, widget=widget)

.........................(etc)................

             fields_dict[field.name] = field_type(label=field.label,
                                                 required=field.required,
                                                 help_text=field.help_text,

 max_length=field.max_length,
                                                 widget=widget)

    return fields_dict

I am hoping to add an additional field, "Photographers" (from the
'Photographer' model) to AForm so that users can select a Photographer to
become part of an instance of the 'A' model.

Does that make sense?

Thank you very much for any ideas!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick_B  
View profile  
 More options Aug 1 2012, 1:43 pm
From: Nick_B <nickbew...@gmail.com>
Date: Wed, 1 Aug 2012 10:43:27 -0700 (PDT)
Local: Wed, Aug 1 2012 1:43 pm
Subject: Re: Working with a Custom, Dynamic Form (using BaseForm)

Here are my models, if you are interested:

    class Photographer(models.Model):
        name = models.CharField(max_length=20)
        models = models.ManyToManyField('Model')

    class Model(models.Model):
        model = models.CharField(max_length=20, blank=False)
        series = models.ForeignKey('Series', null=True, blank=True)

    class A(models.Model):
        user = models.ForeignKey(User)
        photographer = models.ForeignKey('Photographer', blank=True,
null=True)
        model = models.ForeignKey('Model', blank=True, null=True)

Thanks folks


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Anton Baklanov  
View profile  
 More options Aug 4 2012, 12:37 pm
From: Anton Baklanov <antonbakla...@gmail.com>
Date: Sat, 4 Aug 2012 19:37:30 +0300
Local: Sat, Aug 4 2012 12:37 pm
Subject: Re: Working with a Custom, Dynamic Form (using BaseForm)

> I am hoping to add an additional field, "Photographers" (from the
> 'Photographer' model) to AForm so that users can select a Photographer to
> become part of an instance of the 'A' model.

> Does that make sense?

yes, you need to add field and I'm guessing that ModelChoiceField will work
for you.
https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoic
efield<https://docs.djangoproject.com/en/dev/ref/forms/fields/#modelchoicefield>

looks like you are using some additional layer before django.forms and
don't just create fields directly, so you may need to extend it too.

--
Regards,
Anton Baklanov

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick_B  
View profile  
 More options Aug 13 2012, 2:57 pm
From: Nick_B <nickbew...@gmail.com>
Date: Mon, 13 Aug 2012 11:57:05 -0700 (PDT)
Local: Mon, Aug 13 2012 2:57 pm
Subject: Re: Working with a Custom, Dynamic Form (using BaseForm)

Thanks for the advice Anton,

> looks like you are using some additional layer before django.forms and
> don't just create fields directly, so you may need to extend it too.

Does anyone have any advice or references on extending my form to include
an extra field? After searching and reading for days, I'm still lost on how
to make it happen?

Thanks!
Nick_B


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »