List Info

Thread: Help with form-builder application




Help with form-builder application
user name
2007-12-31 02:41:18
Hi,

I'm new to Django and Python and I would appreciate if anyone could help me out here. I'm trying to create a form-builder program, where users can select the form fields they want (such as textbox, string, or datefield etc) to create a master form which they can send out to others and track their response.

What I want to achieve is this (Please advise me if there's a better method). I want to create a <userform> model, and fields that the user creates are passed in as a dictionary similar to the newforms library documentation ( http://www.djangoproject.com/documentation/newforms/) which wrote something similar to:

data = {'subject': 'hello',
&nbsp; &nbsp; &nbsp; &nbsp;   ;  'message': 'Hi there',
 &nbsp;   ; &nbsp; &nbsp; &nbsp; 'sender': ' fooexample.com">fooexample.com',
&nbsp; &nbsp;   ; &nbsp; &nbsp;  'cc_myself': True}
&nbsp; &nbsp; &nbsp; &nbsp;   ; &nbsp; f = ContactForm(data)

The main problem I have is that I am not sure what values to use for the dictionary keys when defining my <userform> class. I tried passing in an empty class as below, in the hope that I can pass to it, a dictionary containing the values and keys a user designed.

class Userform (forms.Form):
 &nbsp;  pass
 ; &nbsp;
When I tried to run the command (python manage.py syncdb), the table cannot be created. The SQL command are just

Begin;
Commit;


How can I design my userform model? The examples and tutorials I've read through so far only mentioned cases where the attributes of the tables we create are known. For example,

class Choice(models.Model):
 &nbsp;  poll = models.ForeignKey(Poll)
 &nbsp;  choice = models.CharField(max_length=200)
 &nbsp;  votes = models.IntegerField()
 &nbsp; 
The choice of attributes are clear cut in this example because it is up to the developer to decide the required table attributes. However in my case, the attributes of the table are set by the user and I have no idea what his/her attributes are.

I would appreciate any advice or feedback on this, and any readings which you think would be helpful.

Regards,
Vincent Woon

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users"; group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to django-users-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Re: Help with form-builder application
country flaguser name
United States
2007-12-31 02:52:42
Actually, I just got an idea for a simple website, and it
would need the
same thing accomplished. I haven't had any time to look into
it yet, but
I know that python has the ability to generate classes and
methods on
the fly. I have never used it, but I am guessing it will be
possible to
create form instances on the fly.
Storing their results would be a bit ugly, unless you wanted
to create
new tables for each time a user creates a new form. The way
I can think
to do it would be to pickle the form instance, and unpickle
it again.
Pickling is another python feature that I haven't looked
into yet
either. Hopefully that is the correct direction to head to
accomplish this.

Cheers!

Jeff Anderson

Vincent Woon wrote:
> Hi,
>
> I'm new to Django and Python and I would appreciate if
anyone could
> help me out here. I'm trying to create a form-builder
program, where
> users can select the form fields they want (such as
textbox, string,
> or datefield etc) to create a master form which they
can send out to
> others and track their response.
>
> What I want to achieve is this (Please advise me if
there's a better
> method). I want to create a <userform> model, and
fields that the user
> creates are passed in as a dictionary similar to the
newforms library
> documentation ( 
http://www.djangoproject.com/documentation/newforms/)
> which wrote something similar to:
>
> data = {'subject': 'hello',
>             'message': 'Hi there',
>             'sender': 'fooexample.com
<mailto:fooexample.com>',
>             'cc_myself': True}
>              f = ContactForm(data)
>
> The main problem I have is that I am not sure what
values to use for
> the dictionary keys when defining my <userform>
class. I tried passing
> in an empty class as below, in the hope that I can pass
to it, a
> dictionary containing the values and keys a user
designed.
>
> class Userform (forms.Form):
>     pass
>    
> When I tried to run the command (python manage.py
syncdb), the table
> cannot be created. The SQL command are just
>
> Begin;
> Commit;
>
>
> How can I design my userform model? The examples and
tutorials I've
> read through so far only mentioned cases where the
attributes of the
> tables we create are known. For example,
>
> class Choice(models.Model):
>     poll = models.ForeignKey(Poll)
>     choice = models.CharField(max_length=200)
>     votes = models.IntegerField()
>    
> The choice of attributes are clear cut in this example
because it is
> up to the developer to decide the required table
attributes. However
> in my case, the attributes of the table are set by the
user and I have
> no idea what his/her attributes are.
>
> I would appreciate any advice or feedback on this, and
any readings
> which you think would be helpful. 
>
> Regards,
> Vincent Woon
>
> --~--~---------~--~----~------------~-------~--~----~
> You received this message because you are subscribed to
the Google
> Groups "Django users" group.
> To post to this group, send email to django-usersgooglegroups.com
> To unsubscribe from this group, send email to
> django-users-unsubscribegooglegroups.com
> For more options, visit this group at
> htt
p://groups.google.com/group/django-users?hl=en
> -~----------~----~----~----~------~----~------~--~---

Re: Help with form-builder application
user name
2007-12-31 10:42:55
> When I tried to run the command (python manage.py
syncdb), the table cannot
> be created. The SQL command are just
>
> Begin;
> Commit;

Your form definitions have nothing to do with the database. 
They are
completely separate things.  There are a few helpers that
combine the
two for convenience, but they are independent of each other
normally.

In answer to your question, I'll tell you how I did
something
similar.

Make a basic model with a 'pickled' textfield.  The model
fields
represent all the static/searchable fields.  Any fields that
will
always be there should be a model field. The pickled field
contains a
serialized python dict with all of the changeable fields. 
The dict is
something like:

{'fieldname': value1, 'fieldname': value2} where fieldname
is a key
into a FieldDef model/table that has meta information about
that field
type like formfield, attrs, validation information,etc. (if
you have a
smaller, static set it wouldn't even need a model, just a
lookup dict)

You can build the form 'on the fly' by using the information
in the
FieldDef model something like this:

class GeneratedBaseForm(forms.BaseForm):
    def save(instance,*args,**kwargs):
         ... do something here to look at
self.fieldmeta/instance/
clean_data and save accordingly

def make_form(fieldmeta):
    base_fields={}
    for f in fieldmeta:
        base_fields[f.name] = getattr(forms,f.formfield)()
    return
type('GeneratedForm',(GeneratedBaseForm,),{''fieldmeta':
fieldmeta,base_fields': base_fields })

fieldmeta =
FieldDef.objects.filter(name__in=['fieldname1','fieldname2']
)
Form = make_form(fieldmeta)
form=Form()

If you write the save method on the baseform carefully, you
can have
it look at the instance model._meta and update the real
model fields,
then pickle the rest of clean_data and store it in the
pickle field.
Same with __init__.

I also tried using a FieldDef model, Base model, and a Field
model:

class MyModel(models.Model):
   ....
class FieldDef(models.Model):
    formfield=models.CharField()
    ... bunch of meta fields

class Field(models.Model):
    fielddef=models.ForeignKey(FieldDef)
    instance=models.ForeignKey(MyModel)

instance=MyModel.objects.get(pk=1)
fields=instance.field_set.all()

Then just step through fields, checking
field.fielddef.formfield and
other meta information and building the form like above.  It
worked
better, because all fields were searchable... but when I got
to
several million field rows the slow performance started to
show.
Using the serialized fields was much, much faster for what I
 was
doing..


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Help with form-builder application
country flaguser name
United States
2008-01-01 18:27:31
I was reading and poking around, and I found this:
http://code.djangoproject.com/wiki/CookBookNewFo
rmsDynamicFields
I believe it explains how to accomplish what you want to
do.

Cheers!

Jeff Anderson

Jeff Anderson wrote:
> Actually, I just got an idea for a simple website, and
it would need the
> same thing accomplished. I haven't had any time to look
into it yet, but
> I know that python has the ability to generate classes
and methods on
> the fly. I have never used it, but I am guessing it
will be possible to
> create form instances on the fly.
> Storing their results would be a bit ugly, unless you
wanted to create
> new tables for each time a user creates a new form. The
way I can think
> to do it would be to pickle the form instance, and
unpickle it again.
> Pickling is another python feature that I haven't
looked into yet
> either. Hopefully that is the correct direction to head
to accomplish this.
>
> Cheers!
>
> Jeff Anderson
>
> Vincent Woon wrote:
>   
>> Hi,
>>
>> I'm new to Django and Python and I would appreciate
if anyone could
>> help me out here. I'm trying to create a
form-builder program, where
>> users can select the form fields they want (such as
textbox, string,
>> or datefield etc) to create a master form which
they can send out to
>> others and track their response.
>>
>> What I want to achieve is this (Please advise me if
there's a better
>> method). I want to create a <userform> model,
and fields that the user
>> creates are passed in as a dictionary similar to
the newforms library
>> documentation ( 
http://www.djangoproject.com/documentation/newforms/)
>> which wrote something similar to:
>>
>> data = {'subject': 'hello',
>>             'message': 'Hi there',
>>             'sender': 'fooexample.com
<mailto:fooexample.com>',
>>             'cc_myself': True}
>>              f = ContactForm(data)
>>
>> The main problem I have is that I am not sure what
values to use for
>> the dictionary keys when defining my
<userform> class. I tried passing
>> in an empty class as below, in the hope that I can
pass to it, a
>> dictionary containing the values and keys a user
designed.
>>
>> class Userform (forms.Form):
>>     pass
>>    
>> When I tried to run the command (python manage.py
syncdb), the table
>> cannot be created. The SQL command are just
>>
>> Begin;
>> Commit;
>>
>>
>> How can I design my userform model? The examples
and tutorials I've
>> read through so far only mentioned cases where the
attributes of the
>> tables we create are known. For example,
>>
>> class Choice(models.Model):
>>     poll = models.ForeignKey(Poll)
>>     choice = models.CharField(max_length=200)
>>     votes = models.IntegerField()
>>    
>> The choice of attributes are clear cut in this
example because it is
>> up to the developer to decide the required table
attributes. However
>> in my case, the attributes of the table are set by
the user and I have
>> no idea what his/her attributes are.
>>
>> I would appreciate any advice or feedback on this,
and any readings
>> which you think would be helpful. 
>>
>> Regards,
>> Vincent Woon
>>
>>
--~--~---------~--~----~------------~-------~--~----~
>> You received this message because you are
subscribed to the Google
>> Groups "Django users" group.
>> To post to this group, send email to
django-usersgooglegroups.com
>> To unsubscribe from this group, send email to
>> django-users-unsubscribegooglegroups.com
>> For more options, visit this group at
>> htt
p://groups.google.com/group/django-users?hl=en
>>
-~----------~----~----~----~------~----~------~--~---
>>     
>
>   


Re: Help with form-builder application
user name
2008-01-02 07:35:55
Hi Jeff and Doug

Thank you for your prompt and helpful responses! I have to
delay my
reply because I'm still figuring out certain core concepts
of Python
that I need to read on before I can reply. 

Jeff:

> Storing their results would be a bit ugly, unless you
wanted to create new tables for each time a user creates a
new form.

Yes I plan to create a new table each time a user creates  a
new form,
but I was stuck because I have no idea how to proceed with
custom
columns for the table.

> Pickling is another python feature that I haven't
looked into yet either.

I've looked into python pickling since both you and Doug
used it, and
I think it's definitely helpful!

> http://code.djangoproject.com/wiki/CookBookNewFo
rmsDynamicFields

Thank you, that looks good! It looks very similar to what I
want to
do. And I realized I have yet to go through the wiki
resources yet
(only checked the documentation, the API, the online book
and Python
docs previously). I will try out the other resources on the
wiki.

Doug:
> Your form definitions have nothing to do with the
database.  They are completely separate things.

I understood that somewhat, but I'm still not too clear of
the
relationships and differences between forms, models and
databases in
Django. I read the newforms library (http://www.djangoprojec
t.com/
documentation/newforms/) and the django overview
documentation (http://
www.djangoproject.com/documentation/overview/). To me, it
appears that
the difference between them is their superclass. The model
class
inherits from models.Model, and the forms class inherit
from
forms.Form.

Let me make a wild guess based on my understanding so far.
Classes
inherited from the Model can store the data in the table of
the
database, while classes inherited from the Form can only
receive/send
data via dictionary mappings, and store them only when they
are
pickled (Thank you Jeff and Doug for introducing this
concept to me).

> Make a basic model with a 'pickled' textfield.  The
model fields represent all the static/searchable fields.

What is the difference between static and searchable fields?
Any links
that would be good reading for me to understand the
concepts?

>		... do something here to look at
self.fieldmeta/instance/

I don't know what this means yet to ask any insightful
questions, but
I figure it should be quite troublesome for one to explain.
I will
read up more, and will ask about it again once I have a
deeper level
of understanding of Django.

>		return
type('GeneratedForm',(GeneratedBaseForm,),{''fieldmeta':
fieldmeta,base_fields': base_fields })

I get the hang of some of this, but I can't explain it
clearly for
now. From my understanding, this passes a dictionary of data
values
for the field meta and base fields to the forms to create
it. I still
have to further read on what the "type" method
does in here (Have not
gotten around to reading the docs for the type method yet).

> fieldmeta =
FieldDef.objects.filter(name__in=['fieldname1','fieldname2']
)
> Form = make_form(fieldmeta)
> form=Form()

> If you write the save method on the baseform
carefully,

Correct me if I'm wrong - This saved data will be stored as
a pickle,
and not saved in the database? Does that mean I will have to
create a
new dump file for each new form created?

Thank you for your time Doug. Your email has opened me up to
more
concepts to learn in Python and Django for me to proceed.


Cheers!
Vincent
Happy new year!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Help with form-builder application
user name
2008-01-02 07:36:30
Hi Jeff and Doug

Thank you for your prompt and helpful responses! I have to
delay my
reply because I'm still figuring out certain core concepts
of Python
that I need to read on before I can reply. 

Jeff:

> Storing their results would be a bit ugly, unless you
wanted to create new tables for each time a user creates a
new form.

Yes I plan to create a new table each time a user creates  a
new form,
but I was stuck because I have no idea how to proceed with
custom
columns for the table.

> Pickling is another python feature that I haven't
looked into yet either.

I've looked into python pickling since both you and Doug
used it, and
I think it's definitely helpful!

> http://code.djangoproject.com/wiki/CookBookNewFo
rmsDynamicFields

Thank you, that looks good! It looks very similar to what I
want to
do. And I realized I have yet to go through the wiki
resources yet
(only checked the documentation, the API, the online book
and Python
docs previously). I will try out the other resources on the
wiki.

Doug:
> Your form definitions have nothing to do with the
database.  They are completely separate things.

I understood that somewhat, but I'm still not too clear of
the
relationships and differences between forms, models and
databases in
Django. I read the newforms library (http://www.djangoprojec
t.com/
documentation/newforms/) and the django overview
documentation (http://
www.djangoproject.com/documentation/overview/). To me, it
appears that
the difference between them is their superclass. The model
class
inherits from models.Model, and the forms class inherit
from
forms.Form.

Let me make a wild guess based on my understanding so far.
Classes
inherited from the Model can store the data in the table of
the
database, while classes inherited from the Form can only
receive/send
data via dictionary mappings, and store them only when they
are
pickled (Thank you Jeff and Doug for introducing this
concept to me).

> Make a basic model with a 'pickled' textfield.  The
model fields represent all the static/searchable fields.

What is the difference between static and searchable fields?
Any links
that would be good reading for me to understand the
concepts?

>		... do something here to look at
self.fieldmeta/instance/

I don't know what this means yet to ask any insightful
questions, but
I figure it should be quite troublesome for one to explain.
I will
read up more, and will ask about it again once I have a
deeper level
of understanding of Django.

>		return
type('GeneratedForm',(GeneratedBaseForm,),{''fieldmeta':
fieldmeta,base_fields': base_fields })

I get the hang of some of this, but I can't explain it
clearly for
now. From my understanding, this passes a dictionary of data
values
for the field meta and base fields to the forms to create
it. I still
have to further read on what the "type" method
does in here (Have not
gotten around to reading the docs for the type method yet).

> fieldmeta =
FieldDef.objects.filter(name__in=['fieldname1','fieldname2']
)
> Form = make_form(fieldmeta)
> form=Form()

> If you write the save method on the baseform
carefully,

Correct me if I'm wrong - This saved data will be stored as
a pickle,
and not saved in the database? Does that mean I will have to
create a
new dump file for each new form created?

Thank you for your time Doug. Your email has opened me up to
more
concepts to learn in Python and Django for me to proceed.


Cheers!
Vincent
Happy new year!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at htt
p://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-6]

about | contact  Other archives ( Real Estate discussion Medical topics )