[PATCH 1/4] forms: Don't attempt to evaluate State at startup

Stephen Finucane stephen at that.guru
Sat May 4 08:21:07 AEST 2019

As was designed, starting the interpreter would cause the State model
and its entries to be evaluated. This was an issue if, for example, the
model had been modified and you were attempting to apply the migration.

  Traceback (most recent call last):
    File "manage.py", line 11, in <module>
    File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 1199, in _set_queryset
      self.widget.choices = self.choices
    File "/home/patchwork/patchwork/patchwork/forms.py", line 157, in _get_choices
      super(OptionalModelChoiceField, self)._get_choices())
    File "/usr/local/lib/python2.7/dist-packages/django/forms/models.py", line 1143, in __len__
      return (len(self.queryset) + (1 if self.field.empty_label is not None else 0))
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 232, in __len__
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1118, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 53, in __iter__
      results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 899, in execute_sql
      raise original_exception
  django.db.utils.OperationalError: (1054, "Unknown column 'patchwork_state.slug' in 'field list'")

Resolve this by moving the evaluation into '__init__', meaning it will
only occur when a new form is created.

Signed-off-by: Stephen Finucane <stephen at that.guru>
 patchwork/forms.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/patchwork/forms.py b/patchwork/forms.py
index 5d4c920a..5690eb01 100644
--- a/patchwork/forms.py
+++ b/patchwork/forms.py
@@ -165,7 +165,6 @@ class OptionalBooleanField(forms.TypedChoiceField):
 class MultiplePatchForm(forms.Form):
     action = 'update'
-    state = OptionalModelChoiceField(queryset=State.objects.all())
     archived = OptionalBooleanField(
         choices=[('*', 'no change'), ('True', 'Archived'),
                  ('False', 'Unarchived')],
@@ -176,6 +175,8 @@ class MultiplePatchForm(forms.Form):
         super(MultiplePatchForm, self).__init__(*args, **kwargs)
         self.fields['delegate'] = OptionalModelChoiceField(
             queryset=_get_delegate_qs(project=project), required=False)
+        self.fields['state'] = OptionalModelChoiceField(
+            queryset=State.objects.all())
     def save(self, instance, commit=True):
         opts = instance.__class__._meta

More information about the Patchwork mailing list