On Tue, 2007-05-29 at 12:19 -0700, ringemup wrote:
> Hello --
> 
> I'm using a basic form_for_model() form object for a model that has a
> unique=True constraint on a field other than the primary key.
> 
> When validating submitted data, is there a reason the form check that
> that constraint hasn't been violated and throw a validation error?
> I'd like to be able to catch that input error and present a message to
> the user, instead of what's currently happening, which is that no
> error is thrown and when I try to save the data I get an integrity
> error, which is unrecoverable.
> 
> Do I need to create a custom form class just to handle a uniqueness
> requirement?

I'm not sure this is completely easy to do with newforms at the moment.
In oldforms, the validators on the model would have caught this, to some
extent, but that isn't the case with newforms.

The short answer to your problem is probably to not use form_for_model()
if you want this sort of support. Instead write your own Form sub-class
and write a clean() method for it. The form_for_model() function is just
an aid after all; it shouldn't be the only thing you ever consider
using.

An long explanation of what's going and how we can fix it follows...

This sort of thing is a motivation for us to get model-aware validation
(which is a catch-all name for a somewhat undefined, but semi-large area
of work) going.

There's a "division of labor" problem going on in the case you are
looking at. Forms are orthogonal to models. It just happens that in this
case, accidentally, your form has a correspondence between its fields
and one of your models, but in the entire universe of possible forms and
possible models, that correspondence can't be assumed.

[Aside: it seems that Django projects split into two rough groups --
those whose forms are in close correlation to their models and those
whose forms map the data onto different fields in different models after
lots of munging; I never seem to write the first type of app, for
example, so form_for_model is something I don't find myself needing at
all.]

So a "uniqueness" constraint on your model is not something that the
form framework is really in a position to check. Instead, what should
happen is you construct the model object and then call
object.validate(), which returns ValidationErrors if there are problems.
The validate() method on a model will have full access to the model
instance (including "self").

There is a lot of the support code in place already for this, because
Adrian started working on it early last year. Other priorities have
taken over in the interim (we've done two releases and newforms has been
written, for a start!), but this is getting closer to the top of the
pile of things to clean up, particularly as we try to round out
newforms. It's not completely trivial, because oldforms still exists
(and will in 1.0, too), so we have to juggle things so that two
approaches work. I know I've done a reasonable about of scribbling on
paper trying to get this straight and I suspect some other people have,
too, over time. We'll work it out, though, and try to make this sort of
thing "more natural" for very simple forms attached to a model.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to