From 110271457d162272183cf13d454bb443191db684 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Mon, 23 Jan 2017 02:40:44 +0100 Subject: [PATCH] Db idx and on_delete for foreignkeys --- contest/migrations/0009_auto_20170123_0139.py | 76 +++++++++++++++++++ contest/models.py | 19 +++-- 2 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 contest/migrations/0009_auto_20170123_0139.py diff --git a/contest/migrations/0009_auto_20170123_0139.py b/contest/migrations/0009_auto_20170123_0139.py new file mode 100644 index 0000000..0c1b1c2 --- /dev/null +++ b/contest/migrations/0009_auto_20170123_0139.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-01-23 01:39 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('contest', '0008_auto_20170119_1202'), + ] + + operations = [ + migrations.AlterField( + model_name='contest', + name='callQrg', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='contest.Frequency'), + ), + migrations.AlterField( + model_name='frequency', + name='note', + field=models.CharField(blank=True, max_length=50), + ), + migrations.AlterField( + model_name='qso', + name='call', + field=models.CharField(db_index=True, max_length=20), + ), + migrations.AlterField( + model_name='qso', + name='otherNo', + field=models.IntegerField(verbose_name='Nr-R'), + ), + migrations.AlterField( + model_name='qso', + name='ownNo', + field=models.IntegerField(verbose_name='Nr-S'), + ), + migrations.AlterField( + model_name='qso', + name='ref', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='contest.Reference'), + ), + migrations.AlterField( + model_name='qso', + name='refStr', + field=models.CharField(max_length=20, verbose_name='EXC'), + ), + migrations.AlterField( + model_name='qso', + name='reportRX', + field=models.CharField(default=59, max_length=7, validators=[django.core.validators.RegexValidator('[1-5][1-9]')], verbose_name='RST-R'), + ), + migrations.AlterField( + model_name='qso', + name='reportTX', + field=models.CharField(default=59, max_length=7, validators=[django.core.validators.RegexValidator('[1-5][1-9]')], verbose_name='RST-S'), + ), + migrations.AlterField( + model_name='reference', + name='name', + field=models.CharField(db_index=True, max_length=20, unique=True), + ), + migrations.AlterField( + model_name='user', + name='ref', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to='contest.Reference'), + ), + migrations.AlterIndexTogether( + name='qso', + index_together=set([('owner', 'call')]), + ), + ] diff --git a/contest/models.py b/contest/models.py index ea7c4af..75c40ca 100644 --- a/contest/models.py +++ b/contest/models.py @@ -9,20 +9,20 @@ from .validators import CallUsernameValidator class Contest(models.Model): name = models.CharField(max_length=20) shortName = models.CharField(max_length=20, unique=True) - callQrg = models.ForeignKey("Frequency", null=True, blank=True) + callQrg = models.ForeignKey("Frequency", models.SET_NULL, null=True, blank=True) def __str__(self): return self.name class Reference(models.Model): - name = models.CharField(max_length=20, unique=True) + name = models.CharField(max_length=20, unique=True, db_index=True) description = models.TextField() def __str__(self): return self.name class User(AbstractUser): - ref = models.ForeignKey(Reference, null=True, blank=True) + ref = models.ForeignKey(Reference, models.SET_NULL,null=True, blank=True) def __init__(self, *args, **kwargs): super(User, self).__init__(*args, **kwargs) @@ -50,13 +50,16 @@ class Frequency(models.Model): class QSO(models.Model): reportValidator = RegexValidator("[1-5][1-9]") - owner = models.ForeignKey(User) + class Meta: + index_together = [ + ["owner", "call"], + ] + + owner = models.ForeignKey(User, db_index=True) time = models.DateTimeField(blank=True) - call = models.CharField(max_length=20) + call = models.CharField(max_length=20, db_index=True) band = models.ForeignKey(Band) - - reportTX = models.CharField(max_length=7, default=59, verbose_name='RST-S', validators=[reportValidator]) reportRX = models.CharField(max_length=7, default=59, verbose_name='RST-R', validators=[reportValidator]) @@ -64,7 +67,7 @@ class QSO(models.Model): otherNo = models.IntegerField(verbose_name='Nr-R') refStr = models.CharField(max_length=20, verbose_name="EXC") - ref = models.ForeignKey(Reference, null=True, blank=True) + ref = models.ForeignKey(Reference, models.SET_NULL, null=True, blank=True) remarks = models.CharField(max_length=50, blank=True)