Model() 得到了一个意外的关键字参数 'instance'

Model() got an unexpected keyword argument 'instance'

我目前正在尝试获取我的模型实例以从中读取并根据模型打印报告

然而,当我尝试到达 URL 时出现上述错误,我假设实例变量不起作用,但我不知道为什么。

我一直在阅读其他文章,发现其他人在使用 Form 而不是 ModelForm 时会出现此错误,所以我不知道这是否与那个相同。

请看下面的代码:

Model.py:

class SettingsClass(models.Model):

    Complex = models.CharField(choices=complex_list , max_length =  22 ,default='1' , unique=True)

    Trial_balance_Year_to_date= models.BooleanField(default = False)
    tbytd_Include_opening_balances=models.BooleanField(default = False)
    tbytd_Only_use_main_accounts=models.BooleanField(default = False)
    tbytd_Print_null_values=models.BooleanField(default = False)
    tbytd_Print_description=models.BooleanField(default = True)
    tbytd_Print_account=models.BooleanField(default = True)
    tbytd_Sort_by_account_name=models.BooleanField(default = True)

    def __str__(self):
        return (self.Complex + ' Settings')

View.py::

def reportsHome(request):
    model = SettingsClass.objects.all().order_by('Complex')

    content ={'model':model }
    return render(request, 'main/reportsHome.html' , content)

def printReports(request , reports_pk):
    pkForm = get_object_or_404(SettingsClass , pk=reports_pk)
    form= SettingsClass(instance=pkForm)

    complexName = form.Complex

    #CHECKING TRIAL BALANCE SETTINGS
    if form.Trial_balance_Year_to_date == True:
        printTrialBalanceYTD = True

        ### Printing Trial Balance PDF
        response = HttpResponse(content_type= 'application/pdf')
        response['Content-Disposition']= 'attachment; filename=TrialBalance' + \
            str(datetime.now()) + '.pdf'
        response['Content-Transfer-Encoding'] = 'binary'

        #SQL STATEMENT
        baseSelect = 'SELECT '+ op5 + op4 + ' Debit , Credit FROM [?].[dbo].[PostGL] AS genLedger '
        xtrbYTD = baseSelect + trbYTD + op1 + op2 + op3 + op6

        cursor = cnxn.cursor();
        cursor.execute(baseTRBYear, [complexName], [complexName], [complexName], [one_yrs_ago]);
        xAll = cursor.fetchall()
        cursor.close()
        xtrbYTD = []
        for row in xtrbYTD:
            rdict = {}
            rdict["Description"] = row[0]
            rdict["Account"] = row[1]
            rdict["Debit"] = row[2]
            rdict["Credit"] = row[3]
            arr_trbYTD.append(rdict)

        content =  {"arr_trbYTD":arr_trbYTD , 'xCreditTotal':xCreditTotal , 'xDebitTotal':xDebitTotal , 'complexName':complexName , 'openingBalances': openingBalances ,'printZero':printZero}
        html_string=render_to_string('main/pdf-trialbalance.html' , content)
        html=HTML(string=html_string)

        result=html.write_pdf()

        with tempfile.NamedTemporaryFile(delete=True) as output:
            output.write(result)
            output.flush()

            output.seek(0)
            response.write(output.read())

            return response

    else:
        printTrialBalanceYTD = False

Urls.py:

#Reports
path('accConnect' , views.reportsHome, name='reportsHome'),
path('accConnect/printReports/<int:reports_pk>' , views.printReports , name='printReports')

Template.html:

{% extends "main/base.html"%}

{% block content%}
<h1 style=" text-align: center">Reports</h1>
<hr>
 <br>
 <div class="list-group">
     <a href="#" class='list-group-item active'>Print Single Complex's</a>
{% for x in model %}
    <a href="{% url 'printReports' %}" class="list-group-item list-group-item-action" >{{ x.Complex }} Reports</a>
{% endfor %}
</div>
{% endblock %}

这里,pkForm已经是模型的实例SettingsClass

pkForm = get_object_or_404(SettingsClass , pk=reports_pk)

因此,不需要执行此操作,这也是不正确的,因为 SettingsClass 是一个模型并且它没有名称为 instance 的字段(它的字段是 Complex , Trial_balance_Year_to_date, 等等).

form= SettingsClass(instance=pkForm)

也许您要使用的是 class 形式而不是 class SettingsClass 形式。但是根据您使用此变量 form 的方式,似乎您可以将其删除并直接替换为 pkForm