Model-level data integrity constraints?

79 views
Skip to first unread message

William

unread,
Aug 20, 2009, 3:03:58 PM8/20/09
to Django users
From what I read in the Django documentation and elsewhere on the Web,
I do not think it is possible to define arbitrary data integrity
constraints for a model beyond the options offered by each model field
type. Do I understand this correctly?

I can see a few work-arounds. I could create my own custom field type
and do the validation there, although I'm not sure whether that would
let me do validation that refers to other fields (e.g., a constraint
like, "the sum of field A and field B must be less than ten").

I can define my integrity constraints in the validation for the forms
that act on the model, but this clearly breaks the "don't repeat
yourself" principle, as I would have to repeat these constraints in
every form (class) that I write.

I could also override my model's save() method, but I don't think I
would be able do anything about invalid data other than refuse to
write them to the database. I don't think there's an exception

I hope my question is clear. I'm quite the Django novice. Can anyone
please tell me if I'm missing something?

William

unread,
Aug 20, 2009, 3:07:35 PM8/20/09
to Django users
To fix my typo above:

> I could also override my model's save() method, but I don't think I
> would be able do anything about invalid data other than refuse to
> write them to the database. I don't think there's an exception

...that I could throw that would inform the view that called save()
what went wrong.

Russell Keith-Magee

unread,
Aug 20, 2009, 9:06:50 PM8/20/09
to django...@googlegroups.com
On Fri, Aug 21, 2009 at 3:03 AM, William<william...@gmail.com> wrote:
>
> From what I read in the Django documentation and elsewhere on the Web,
> I do not think it is possible to define arbitrary data integrity
> constraints for a model beyond the options offered by each model field
> type. Do I understand this correctly?
>
> I can see a few work-arounds. I could create my own custom field type
> and do the validation there, although I'm not sure whether that would
> let me do validation that refers to other fields (e.g., a constraint
> like, "the sum of field A and field B must be less than ten").

This is a feature that is on it's way. It was originally planned for
v1.0, but had to be deferred. Honza Kral worked on the feature for the
Google Summer of Code this year. If you search the archives for "model
validation", you can find out more details.

> I can define my integrity constraints in the validation for the forms
> that act on the model, but this clearly breaks the "don't repeat
> yourself" principle, as I would have to repeat these constraints in
> every form (class) that I write.
>
> I could also override my model's save() method, but I don't think I
> would be able do anything about invalid data other than refuse to
> write them to the database. I don't think there's an exception
>
> I hope my question is clear. I'm quite the Django novice. Can anyone
> please tell me if I'm missing something?

You've pretty much captured the current situation as well as the ways
that you can respond.

Yes, the current situation is less than ideal, and yes, this is a
feature we want to add - hopefully it will be in v1.2.

Yours,
Russ Magee %-)

William

unread,
Aug 28, 2009, 1:42:08 PM8/28/09
to Django users
On Aug 20, 9:06 pm, Russell Keith-Magee <freakboy3...@gmail.com>
wrote:

> You've pretty much captured the current situation as well as the ways
> that you can respond.
>
> Yes, the current situation is less than ideal, and yes, this is a
> feature we want to add - hopefully it will be in v1.2.

Gotcha. Thank you, Russ.
Reply all
Reply to author
Forward
0 new messages