[PATCH] models: Resolve issues with Patch.state

Stephen Finucane stephen.finucane at intel.com
Wed Jul 22 19:17:13 AEST 2015


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 <stephen.finucane at intel.com>
---
 patchwork/migrations/0001_initial.py                  |  2 +-
 .../migrations/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 65d1c35..812558a 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 0000000..4887935
--- /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 c2b8a9c..0c8022c 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.0.0



More information about the Patchwork mailing list