Upgrading from Django 1.6 to 1.9: python manage.py migrate failure

不想你离开。 提交于 2019-11-29 14:03:28

The problem is that your queryset is being evaluated when the urls.py loads. When you run makemigrations for a new project, this causes the error because the table has not been created yet.

You can fix this by subclassing ListView and moving the queryset into get_queryset.

class MyListView(ListView):
    template_name = 'numeric/apply.html'

    def get_queryset(self):
        return list(chain(models.HowToApply.objects.filter(active=True).order_by('sequence_number'), models.AcademyAdmin.objects.all()))

Then change your url pattern to use your new view.

url(r'^academy/howtoapply/$',
    MyListView.as_view(),
    name='apply',
),

Django 1.9 runs some checks to validate your url patterns, which means that the url patterns are loaded before the makemigrations command runs. Django 1.8 does not have these checks, so you can get away with setting the queryset as you have done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!