Tuesday, April 18, 2017

Re: First models.py needs tuning

On 19/04/2017 8:13 AM, Rich Shepard wrote:
> Converting from the postgres schema to a Django models.py for my first
> Django project. Most of the syntax (64 lines in the file) should be
> correct,
> but there are two conditions that I have not found how to handle.
>
> First, a couple of classes have primary keys with three fields. I know
> there's a models.PrimaryKey but haven't found an example of how to apply
> that to a field that foreign key fields.

You probably need a single FK plus a meta option of unique_together

https://docs.djangoproject.com/en/1.8/ref/models/options/#unique-together

>
> 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.8/ref/models/instances/#django.db.models.Model.clean

Cheers

Mike
>
> If you would be willing to look at the module, show me how to write the
> two conditions above, and check syntax on the all 5 classes in it I will
> send you the file off the mail list.
>
> Rich
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f8c37692-2c4c-09bd-5fa3-9189f6313797%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment