Hi Honza, On 12/01/07, Honza Král <[EMAIL PROTECTED]> wrote: > On 1/12/07, Felix Ingram <[EMAIL PROTECTED]> 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
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 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---