On 21/04/2017 7:48 AM, Rich Shepard wrote:
On Wed, 19 Apr 2017, Mike Dewhirst wrote:

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

Mike,

  Okay. What would be the correct syntax for
PRIMARY KEY unique_together=('company', 'person', 'proj_nbr')

In general with SQL you want a single PK per table and constraints for unique-ness. If I recall correctly your original question implied you are starting with an existing database and sort of building a Django project around it. If that is true it becomes a more difficult task. So I can't help without lots more information.

In the more usual scenario you specify your models using Python and let the Django ORM framework do the SQL. If that is your case ...

1. No need for a PK. Django automatically inserts a PK. You can refer to this in your code as model_id or model.id where "model" is the lower-case class name of the model. This is best-practice.

2. If you want to do it yourself you can make almost any field the PK with an option primary_key=True in which case Django won't bother. Do this only if you must. For example if you inherit an existing database with specified PKs.

3. In the case of 1 above I would expect three models being Company, Person and Project each of which would have a PK managed by Django. If so, the model you are designing (possibly Project) presumably has a field called 'proj_nbr' and foreign keys to Company and Person. Those FKs are represented in your model as company = models.ForeignKey('Company') and person = models.ForeignKey('Person')

https://docs.djangoproject.com/en/1.8/topics/db/models/#relationships

4. Within each model is a class Meta: to carry various options for the model as a whole. The option you are looking for is: unique_together = (('company', 'person', 'proj_nbr'),)

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



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

This I'll need to ponder more to figure out where to put the clean method
so it validates entries before they're saved.

I put the clean() method after the save() method which follows the __str__() method which follows the Meta class. It doesn't really matter so long as you are consistent.

Django forms call your model.clean() method before saving but test code needs to call it explicitly or if you are providing an API other callers need to call model.clean() explicitly as well.

https://docs.djangoproject.com/en/1.8/ref/models/instances/#validating-objects

Another place I sometimes use is the save() method. Django also provides pre_save and post_save hooks so you never need to use database triggers.

HTH


Thanks,

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+unsubscr...@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/ac603ed3-a3a3-7435-c07b-75ac1fd283a9%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.

Reply via email to