First time posting here, so please excuse me if my opinions aren't very useful and my bad English.
2010/1/6 Waylan Limberg <way...@gmail.com> > I've only scanned the docs the other day and haven't actually used the > new model validation stuff, so my impressions may be a little off, > but... > > On Wed, Jan 6, 2010 at 2:54 PM, Joseph Kocherhans <jkocherh...@gmail.com> > wrote: > > On Wed, Jan 6, 2010 at 12:49 PM, Simon Willison <si...@simonwillison.net> > wrote: > >> A couple of related tickets filed today about model validation: > >> > >> http://code.djangoproject.com/ticket/12513 > >> http://code.djangoproject.com/ticket/12521 > >> > >> The first one describes the issue best - the new model validation code > >> breaks the following common Django convention: > >> > >> form = SecretQuestionForm( {"secret_question":"foo", "answer":"bar"} ) > >> if form.is_valid(): > >> p = form.save(commit=False) > >> p.user = request.user > >> p.primary_contact = somecontact > >> p.save() > >> > > My initial reaction to this snippet was to wonder why the model was > not being validated after the additional data was added/altered. > Shouldn't you be doing: > > form = SecretQuestionForm( {"secret_question":"foo", "answer":"bar"} ) > if form.is_valid(): > p = form.save(commit=False) > p.user = request.user > p.primary_contact = somecontact > if p.full_validate(): # <<<<< note this line > p.save() > Maybe you could do something like this: with form.valid_model() as model: # i'm not good at inventing names model.user = request.user model.primary_contact = somecontact The valid_model() would be a context manager that does form validation and form.save(commit=False) on enter + model validation and save() on exit. Of course this will only work on Python 2.5+, so it's probably no good for django 1.2. Just wanted to share an idea. > > [snip] > > > Once again, that means ModelForm won't really validate your model, > > only your form. form.save() might still throw a database error just > > like before the merge. We can slap a big warning in the ModelForm docs > > though. > > Well, that's why I expected the extra validation check on the model > itself. I understand the desire to have the ModelForm actually > validate the model and work in one step, but sometimes we need the > other way too (as you acknowledge). > > > One (probably unhelpful) way to make ModelForm validate your whole > > model would be to add a Meta flag to ModelForm: > > > > class UserForm(ModelForm): > > class Meta: > > # Defaults to False > > validate_model = True > > Well, what if one view uses that ModelForm one way and another view > uses the same ModelForm the other way? What about > ``form.is_valid(validate_model=True)``? > This seems like a good idea. > -- > ---- > \X/ /-\ `/ |_ /-\ |\| > Waylan Limberg > > -- > You received this message because you are subscribed to the Google Groups > "Django developers" group. > To post to this group, send email to django-develop...@googlegroups.com. > To unsubscribe from this group, send email to > django-developers+unsubscr...@googlegroups.com<django-developers%2bunsubscr...@googlegroups.com> > . > For more options, visit this group at > http://groups.google.com/group/django-developers?hl=en. > -- Łukasz Rekucki--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-develop...@googlegroups.com.
To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.