Thursday, March 31, 2011

Re: Dynamically generating form fields

I've been fighting to get this to work for me to. I can point you to a few articles that are helping me achieve what I want. There's some black magic involved, but once you play with the code for a while you'll know what it does.


These are some VERY good links with info that should definitely be included in the docs. Good luck and I apologize for not answering sooner. My use case is different from yours, so pasting my code probably won't be that useful to you.


Cheers,
André Terra

On Mon, Mar 28, 2011 at 11:32 AM, fersan <irfsan3@gmail.com> wrote:
Hello,

I am trying to create a form with fields based on types of an object
property. Basically, I am setting up a system which has hardware
'demos' which are categorized by industry. I want to create a form
which has a Checkbox Select Multiple for each industry, with the
choices being the demos for that industry. Currently, my (un-working)
form code looks like:

class select_demo_apps_frm(forms.Form):
# List apps from each industry with selection boxes for each app.
   def __init__(self, *args, **kwargs):
       super(select_demo_apps_frm, self).__init__(*args, **kwargs)

       inds = industries() # returns all industry objects

       for ind in inds:
           fieldname = '%s' % ind.name
           self.fields[fieldname] = forms.CheckboxSelectMultiple()
           self.fields[fieldname].choices = [(d.id, marksafe('<b>%s</
b> - %s') % (d.name, d.desc)) for d in demo.objects.filter(industry =
ind)]

Unfortunately, I'm getting an error from this code:

Error when calling the metaclass bases
   Cannot create a consistent method resolution
order (MRO) for bases Form, select_demo_apps_frm

It could be possible to set a static field for each industry, however
in the interest of long term maintainability I'd rather do it
dynamically in case new industries get added after I leave this job.
Just wondering if anyone can help with creating this form in such a
way that it actually works ;)

Thanks much!

Fernando Sanchez

--
You received this message because you are subscribed to the Google Groups "Django users" group.
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.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
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