#26273: Use of @classmethod in queryset can crash manage.py if a model is 
changed
------------------------------+--------------------------------------------
     Reporter:  scoenye       |      Owner:  nobody
         Type:  Bug           |     Status:  new
    Component:                |    Version:  1.9
  Uncategorized               |
     Severity:  Normal        |   Keywords:  classmethod queryset manage.py
 Triage Stage:  Unreviewed    |  Has patch:  0
Easy pickings:  0             |      UI/UX:  0
------------------------------+--------------------------------------------
 Starting with
 {{{
 class Graduation(models.Model):
     ceremony_date = models.DateField()
     ...

     @classmethod
     def current_graduation(cls):
         early = date.today() - timedelta(183)
         late = date.today() + timedelta(183)

         return cls.objects.get(ceremony_date__gte=early,
 ceremony_date__lte=late)

 class Graduate(models.Model):
     conferral    = models.ForeignKey(Graduation)
     first_name   = models.CharField(max_length = 14)
     last_name    = models.CharField(max_length = 19)
     ...
 }}}
 and
 {{{
 class TestReport(ListView):
     queryset =
 Graduate.objects.filter(conferral=Graduation.current_graduation()) \
                                .order_by("last_name", "first_name")

     template_name       = "graduation/reports/applicants.html"
     context_object_name = "applicants"
 }}}

 The object is to automatically show the applicants for the next upcoming
 graduation. This works like a charm until the Graduation model is
 modified:

 {{{
 class Graduation(models.Model):
     ceremony_date = models.DateField()
     crash_test    = models.CharField(max_length=10)
     ...

     @classmethod
     def current_graduation(cls):
         early = date.today() - timedelta(183)
         late = date.today() + timedelta(183)

         return cls.objects.get(ceremony_date__gte=early,
 ceremony_date__lte=late)
 }}}
 At this point
 {{{
 python manage.py makemigrations
 }}}
 crashes (and so does pretty much any invocation besides asking for --help)

 Full stack trace attached, but the cause appears to be that the entire
 application is loaded, triggering execution of the class method. This in
 turn ends up asking the database for a column that does not exist yet as
 the ORM generates a select including all field names.

 Not defining queryset on the ListView and using get_queryset() fixes the
 problem, but as I originally got bitten by this when rolling a changeset
 out to production, I thought the condition is worth documenting.

--
Ticket URL: <https://code.djangoproject.com/ticket/26273>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.a9ce2b6ad02332df855e5cc2ff26167b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to