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 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/-/nmolpH9zKwAJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
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 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/-/z5Gqc3jVzLsJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
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 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/-/JMg7nHwI44sJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.