From d6e7b50b3f70f908d51feca596cd761545910972 Mon Sep 17 00:00:00 2001 From: Stephen Finucane Date: Wed, 22 Jul 2015 09:34:58 +0100 Subject: [PATCH] models: Resolve issues with Patch.state The initial migration introduced in '30bb271' was incomplete: running 'makemigrations' on the current codebase will produce a migration which could not be applied cleanly. The reason for this was the non-loading of initial data for the 'State' model. Fix this issue by only referencing the contents of the 'State' model when we have to (on save of a 'Patch' object), thus allowing the user suitable time to apply this initial data. Signed-off-by: Stephen Finucane Acked-by: Damien Lespiau --- patchwork/migrations/0001_initial.py | 2 +- .../0002_fix_patch_state_default_values.py | 19 +++++++++++++++++++ patchwork/models.py | 8 +++----- 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 patchwork/migrations/0002_fix_patch_state_default_values.py diff --git a/patchwork/migrations/0001_initial.py b/patchwork/migrations/0001_initial.py index 65d1c355..812558a2 100644 --- a/patchwork/migrations/0001_initial.py +++ b/patchwork/migrations/0001_initial.py @@ -2,12 +2,12 @@ from __future__ import unicode_literals from django.db import models, migrations -from django.core.management import call_command import datetime import patchwork.models import django.db.models.deletion from django.conf import settings + class Migration(migrations.Migration): dependencies = [ diff --git a/patchwork/migrations/0002_fix_patch_state_default_values.py b/patchwork/migrations/0002_fix_patch_state_default_values.py new file mode 100644 index 00000000..48879350 --- /dev/null +++ b/patchwork/migrations/0002_fix_patch_state_default_values.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models, migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('patchwork', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='patch', + name='state', + field=models.ForeignKey(to='patchwork.State', null=True), + ), + ] diff --git a/patchwork/models.py b/patchwork/models.py index c2b8a9c9..0c8022cf 100644 --- a/patchwork/models.py +++ b/patchwork/models.py @@ -243,7 +243,7 @@ class Patch(models.Model): date = models.DateTimeField(default=datetime.datetime.now) submitter = models.ForeignKey(Person) delegate = models.ForeignKey(User, blank = True, null = True) - state = models.ForeignKey(State, default=get_default_initial_patch_state) + state = models.ForeignKey(State, null=True) archived = models.BooleanField(default = False) headers = models.TextField(blank = True) content = models.TextField(null = True, blank = True) @@ -279,10 +279,8 @@ class Patch(models.Model): self._set_tag(tag, counter[tag]) def save(self): - try: - s = self.state - except: - self.state = State.objects.get(ordering = 0) + if not hasattr(self, 'state') or not self.state: + self.state = get_default_initial_patch_state() if self.hash is None and self.content is not None: self.hash = hash_patch(self.content).hexdigest() -- 2.47.3