I have following error when I run the code. Can any of you please help to me solve the issue?
AttributeError: 'str' object has no attribute '_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
Views.py
def qc3_approval_view(request):
cn_data_form = qc3_approval_form( instance=request.GET['key'] )
forms.py
class qc3_approval_form(forms.ModelForm ):
class Meta:
model = cn_data_tables
fields = ('status_qc3',)
models.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
I changed views.py as below and it is working.
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)
Your problem is caused because you are not providing an instance to qc3_approval_form
, instead, you are providing a string, which does not have the _meta
attribute.
Changing the views.py to provide an instance to the form should work.
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)
The argument of the register func is not a str, just write the name of the model. The string has not that attribute, but the function does. so, in the admin.py file write:
admin.site.register(cn_data.tables)
that just happend to me and that worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.