Thursday, June 29, 2017

Re: First models.py needs tuning

On Wed, 19 Apr 2017, Mike Dewhirst wrote:

>> Second, I have a couple of fields with postgres check constraints; that
>> is, valid data is restricted to a list of strings. I've not found a
>> Django model field validator teaching me how to write this.

> Use the model's clean() method ...
> https://docs.djangoproject.com/en/1.11/ref/models/instances/#django.db.models.Model.clean

What I see in Model.clean is a validation check which returns an error
message to the user if the check fails. I understand using this in cases
where required data are missing (such as the example of a draft article not
having a blank publication date). What I don't see is how to write a
validation check for an attribute whose value should be limited to a list.

Example: a table attribute (column) for 'industry' in which valid entries
are limited to a list such as 'Agriculture', 'Business, other', 'Chemicals',
'Energy', 'Law').

In postgres SQL this would be a constraint check. How do I write this in a
django Model.clean or its relatives?

Rich

No comments:

Post a Comment