Rich
I've run out of time this week. Maybe someone else can help.
Mike
On 21/04/2017 11:45 AM, Rich Shepard wrote:
> On Fri, 21 Apr 2017, Mike Dewhirst wrote:
>
>> 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 ...
>
> Mike,
>
> I wrote the schema for postgres but had not created the database. So I
> renamed the file models.py and converted the tables to classes.
>
>> 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.
>
> I understand that Django creates the PK when a class has only a single
> variable. I have a table that contains one FK and two other variables
> form a
> combined primary key. Perhaps this is a case for the many-to-one model
> type
> in Django?
>
>> 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')
>
>> 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'),)
>
> Here's one class which requires three columns to make each row unique:
>
> class Projects (models.Model):
> company=models.ForeignKeyField('Companies', on_delete=models.CASCADE)
> person=models.ForeignKeyField('Contacts', on_delete=models.CASCADE)
> proj_nbr=models.CharField(max_length=8, Field_null=True)
> proj_name=models.TextField(Field_null=True)
> description=models.TextField()
> PRIMARY KEY unique_together=('company', 'person', 'proj_nbr')
>
> If I delete 'PRIMARY KEY' and leave the rest of the line, does this
> provide the relational integrity?
>
>>> 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.
>
> Okay. I'll try writing these methods after I get the multi-variable PKs
> correct.
>
>> 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.
>
> This goes beyond what I've learned. I'll get there step-by-step.
>
> Yes, you are helping me smooth off the rough spots.
>
> 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+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/f0163c1e-ff69-6dfd-bbb7-e0faa224c04a%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment