Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Newforms 'preferred practice'
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Felix Ingram  
View profile  
 More options Jan 12 2007, 7:02 am
From: "Felix Ingram" <f.ingram.li...@gmail.com>
Date: Fri, 12 Jan 2007 12:02:10 +0000
Local: Fri, Jan 12 2007 7:02 am
Subject: Newforms 'preferred practice'
Hello all,

I've been playing around with the newforms library and I'd like to
check that I'm on the right track.

I've got a fairly complicated model that has a few foreign keys and
m2m fields. One of the m2m fields references a table with ~130,000
rows so the usual select boxes aren't that usable. I've created a form
which simplifies the entry but I would like to check the preferred way
of doing certain things.

1. Additional validation - One field is a RegexField but I would like
to add extra validation. At the moment I've subclassed it and added my
own 'clean' method.

2. Saving the model - I've implemented a save method in the form which
will return an instance of the model in a similar fashion to
form_for_model. Is this good practice or should I create the instance
in my view and populate it from the form?

3. Binding data - I'd like the form to handle editing of an instance.
I could construct a dictionary of values and pass it to the form as
per the docs but I'd like to just pass a model instance and have the
form sort things out. Is this advisable? If so then does anyone have
any pointers about what methods I need to implement?

Thanks in advance for any pointers.

Felix

P.S. Are there any thoughts about form inheritance? I'd like to be
able to create a form base on another but with some more fields. At
the moment this doesn't seem to work as expected...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Honza Král  
View profile  
 More options Jan 12 2007, 8:17 am
From: "Honza Král" <honza.k...@gmail.com>
Date: Fri, 12 Jan 2007 14:17:25 +0100
Local: Fri, Jan 12 2007 8:17 am
Subject: Re: Newforms 'preferred practice'
On 1/12/07, Felix Ingram <f.ingram.li...@gmail.com> wrote:

> Hello all,

> I've been playing around with the newforms library and I'd like to
> check that I'm on the right track.

> I've got a fairly complicated model that has a few foreign keys and
> m2m fields. One of the m2m fields references a table with ~130,000
> rows so the usual select boxes aren't that usable. I've created a form
> which simplifies the entry but I would like to check the preferred way
> of doing certain things.

> 1. Additional validation - One field is a RegexField but I would like
> to add extra validation. At the moment I've subclassed it and added my
> own 'clean' method.

You can either override clean (remember to call the super.clean) on
the field, or implement clean_FIELD on the form

> 2. Saving the model - I've implemented a save method in the form which
> will return an instance of the model in a similar fashion to
> form_for_model. Is this good practice or should I create the instance
> in my view and populate it from the form?

I do the same thing since I reuse the form in multiple views

> 3. Binding data - I'd like the form to handle editing of an instance.
> I could construct a dictionary of values and pass it to the form as
> per the docs but I'd like to just pass a model instance and have the
> form sort things out. Is this advisable? If so then does anyone have
> any pointers about what methods I need to implement?

the easiest way how to do this is in __init__ of the form create the
dictionary from the model and use it when calling super.__init__

> Thanks in advance for any pointers.

> Felix

> P.S. Are there any thoughts about form inheritance? I'd like to be
> able to create a form base on another but with some more fields. At
> the moment this doesn't seem to work as expected...

it works but only for dynamic fields added via self.fields['name'] =
forms.Field(...)
I use it with success


--
Honza Král
E-Mail: Honza.K...@gmail.com
ICQ#:   107471613
Phone:  +420 606 678585

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rose  
View profile  
 More options Jan 12 2007, 8:50 am
From: "Chris Rose" <ch...@vault5.com>
Date: Fri, 12 Jan 2007 05:50:35 -0800
Local: Fri, Jan 12 2007 8:50 am
Subject: Re: Newforms 'preferred practice'

Felix Ingram wrote:
> 3. Binding data - I'd like the form to handle editing of an instance.
> I could construct a dictionary of values and pass it to the form as
> per the docs but I'd like to just pass a model instance and have the
> form sort things out. Is this advisable? If so then does anyone have
> any pointers about what methods I need to implement?

You can use the model instances __dict__:

my_form = CreateForm(initial=model_instance.__dict__)

Saves copying each field into a seperate dict or implementing any extra
methods in your form.

Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Felix Ingram  
View profile  
 More options Jan 12 2007, 9:49 am
From: "Felix Ingram" <f.ingram.li...@gmail.com>
Date: Fri, 12 Jan 2007 14:49:35 +0000
Local: Fri, Jan 12 2007 9:49 am
Subject: Re: Newforms 'preferred practice'
Hi Honza,

On 12/01/07, Honza Král <honza.k...@gmail.com> wrote:

Good, I'll stick with the subclassing. This seems to work well enough.

> > 2. Saving the model - I've implemented a save method in the form which
> > will return an instance of the model in a similar fashion to
> > form_for_model. Is this good practice or should I create the instance
> > in my view and populate it from the form?

> I do the same thing since I reuse the form in multiple views

This does seem to work, but not I'm getting into trouble when I bind
it to an instance.

> > 3. Binding data - I'd like the form to handle editing of an instance.
> > I could construct a dictionary of values and pass it to the form as
> > per the docs but I'd like to just pass a model instance and have the
> > form sort things out. Is this advisable? If so then does anyone have
> > any pointers about what methods I need to implement?

> the easiest way how to do this is in __init__ of the form create the
> dictionary from the model and use it when calling super.__init__

I've just implemented this but I run into trouble when I update the
model. At the moment I'm passing in the POST dict but things are
obviously going wrong as the form fails to validate but I don't get
any errors.

Would it be possible for you to paste an example from one of your
form's __init__s? I have a feeling that I'm missing something obvious.

> > P.S. Are there any thoughts about form inheritance? I'd like to be
> > able to create a form base on another but with some more fields. At
> > the moment this doesn't seem to work as expected...

> it works but only for dynamic fields added via self.fields['name'] =
> forms.Field(...)
> I use it with success

I'll give this a go; thanks.

Felix


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Felix Ingram  
View profile  
 More options Jan 12 2007, 9:53 am
From: "Felix Ingram" <f.ingram.li...@gmail.com>
Date: Fri, 12 Jan 2007 14:53:04 +0000
Local: Fri, Jan 12 2007 9:53 am
Subject: Re: Newforms 'preferred practice'
Hi Chris,

On 12/01/07, Chris Rose <ch...@vault5.com> wrote:

> Felix Ingram wrote:
> > 3. Binding data - I'd like the form to handle editing of an instance.
> > I could construct a dictionary of values and pass it to the form as
> > per the docs but I'd like to just pass a model instance and have the
> > form sort things out. Is this advisable? If so then does anyone have
> > any pointers about what methods I need to implement?

> You can use the model instances __dict__:

> my_form = CreateForm(initial=model_instance.__dict__)

> Saves copying each field into a seperate dict or implementing any extra
> methods in your form.

This works quite well for simple forms but it won't populate m2m
fields and I need to tweak some fields for display.

Thanks for reminding me about __dict__ though. I do forget about it.

Regards,

Felix


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »