﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26273	Module level queries in a project can crash migrations if the queried model schema changed.	Sven Coenye	nobody	"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."	Bug	closed	Uncategorized	1.9	Normal	invalid	classmethod queryset manage.py		Unreviewed	0	0	0	0	0	0
