Migration clashes with forms.py

前端 未结 5 551
轮回少年
轮回少年 2020-12-11 05:12

The command python manage.py makemigrations fails most of time due to the forms.py, in which new models or new fields are referenced at class defin

5条回答
  •  甜味超标
    2020-12-11 05:57

    I had a similar issue with ModelChoiceField in one of my forms. I had to comment out my forms code in order to be able to make migrations.

    For me the solution was to move all form imports into their respective view method in views.py.

    before:

    from .forms import CalculatorForm
    
    def calculator(request):            
        if request.method != 'POST':
            form = CalculatorForm()
            # ...
    

    after:

    def calculator(request):
        from .forms import CalculatorForm
        if request.method != 'POST':
            form = CalculatorForm()
            # ...
    

提交回复
热议问题