django 模型到指令:属性错误:"str"对象没有属性"_meta"

bn31dyow  于 2022-12-30  发布在  Go
关注(0)|答案(3)|浏览(221)

我有以下错误时,我运行的代码。可以请任何人帮助我解决这个问题?
属性错误:"str"对象没有属性"_meta"

Traceback (most recent call last):
  File "C:\Users\ipcramk\PycharmProjects\Quality_Check_UI\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\ipcramk\PycharmProjects\Quality_Check_UI\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\ipcramk\PycharmProjects\Quality_Check_UI\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\ipcramk\PycharmProjects\Quality_Check_UI\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\ipcramk\PycharmProjects\Quality_Check_UI\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "C:\Users\ipcramk\Desktop\Desktop files\pYTHON TRAINING\Django\Quality_Check_UI\QC_project\app1\views.py", line 328, in qc3_approval_view
    cn_data_form = qc3_approval_form( instance=request.GET['key'] )
  File "C:\Users\ipcramk\PycharmProjects\Quality_Check_UI\lib\site-packages\django\forms\models.py", line 292, in __init__
    object_data = model_to_dict(instance, opts.fields, opts.exclude)
  File "C:\Users\ipcramk\PycharmProjects\Quality_Check_UI\lib\site-packages\django\forms\models.py", line 82, in model_to_dict
    opts = instance._meta
AttributeError: 'str' object has no attribute '_meta'
[24/Oct/2021 12:02:40] "GET /cn_entry/approval_page?key=2 HTTP/1.1" 500 92849

浏览次数. py

def qc3_approval_view(request):
    cn_data_form = qc3_approval_form( instance=request.GET['key'] )

表单. py

class qc3_approval_form(forms.ModelForm ):

    class Meta:
        model = cn_data_tables
        fields = ('status_qc3',)

型号. py

class cn_data_tables(models.Model):
    sl_no = models.AutoField(primary_key=True)
    cn_number = models.CharField(max_length=10, null=False)
    status_qc3 = models.ForeignKey( 'status_list_table',related_name='status_qc3', on_delete=models.CASCADE, default=3 )

    def __int__(self):
        return self.sl_no
3pvhb19x

3pvhb19x1#

出现此问题的原因是,您没有为qc3_approval_form提供示例,而是提供了一个没有_meta属性的字符串。
更改views.py以向表单提供示例应该可以工作。

def qc3_approval_view(request):
    cn_data= cn_data_tables.objects.filter(sl_no=request.GET['key']).first()
    cn_data_form = qc3_approval_form( instance=cn_data)
aemubtdh

aemubtdh2#

我修改了views.py,如下所示,它正在工作。

def qc3_approval_view(request):
    cn_data= cn_data_tables.objects.filter(sl_no=request.GET['key']).first()
    cn_data_form = qc3_approval_form( instance=cn_data)
1u4esq0p

1u4esq0p3#

寄存器func的参数不是一个字符串,只写模型的名字。字符串没有这个属性,但是函数有。所以,在admin.py文件中写:
第一个月
这就发生在我身上,而且奏效了

相关问题