Friday, April 21, 2017

Re: First models.py needs tuning

On Fri, 21 Apr 2017, Michal Petrucha wrote:

> If you want to be able to gracefully handle violations of those
> constraints, and produce more meaningful error messages (for example, when
> validating a form that processes one of these models), you'd also
> implement those checks in Python, either in your models' clean methods, or
> if it's a constraint only involving a single field, you can also just add
> a custom validator to that field.

Michal,

Yes, I can see validating data entered by users in the view or a class
method. I'm used to letting postgres do as much work as possible because
it's quicker and cleaner. So I adapt my thinking and learn to do it Django's
way.

There are two fields in one class where data needs to be restricted to
certain values. I'll learn to write custom validators for those two fields.

>> I'll let Django do this. However, should I still specify
>> unique_together() for the columns that would have comprised the PK?
>
> Indeed – if you want to be certain that those natural keys are really
> keys, you should list them in unique_together. That will enforce
> uniqueness both with UNIQUE constraints in the table definition, as
> well as higher-level validation in Python.

As I thought. I did not think of adding the UNIQUE constraint to the
appropriate fields, but will.

Many thanks,

Rich

No comments:

Post a Comment