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:
On Tue, Jul 31, 2012 at 8:14 PM, Nick_B <nickbew...@gmail.com> wrote:
> 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:
> # 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])
> 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.
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)
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.
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)
> 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.
> 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.
> 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?