views:

36

answers:

1
+1  Q: 

django forms doubt

Here, I am a bit confused with forms in Django. I have information for the form(a poll i.e the poll question and options) coming from some db_table - table1 or say class1 in models. Now the vote from this poll is to be captured which is another model say class2. So, I am just getting confused with the whole flow of forms, here i think. How will the data be captured into the class2 table?

I was trying something like this.

def blah1()
    get_data_from_db_table_1()
    x = blah2Form()
    render_to_response(blah.html,{...})

+3  A: 

Forms have nothing to do with models in Django. They are just class meant to get informations from a dictionary (often request.POST) and check if each data linked to a key match a type and a format (e.g: is this a string of the form "[email protected]").

You can ask django to create a form from a model, and in that case it will do its checking job, then if the data match, it will create a model, fill it and save it.

If a form is not created from a model, it will do nothing but checking. It will save nothing.

If it is created from a model, it will create a new instance of this particular model instance and save it.

If you want something more complicated, like, pre fill a form from various models or according to some conditions, or, say, you need to save several models according to the result of one form, you must do it manually.

e-satis
Thanks for that. How do I go about saving instances in cases of forms which aren't part of any model and involves including data in various tables(may be various models) ?
zubinmehta
Either you override the "save()" method of your form and you do the dirty work here, or you connect a callback function doing it to the "post_save" signal of your form.
e-satis