typeError: isinstance() arg 2 must be a type or tuple of types >>>
Question:
>>> names=['jill','jack']
>>> isinstance(names,list)
Traceback (most recent call last):
File "<pyshell#291>", line 1, in <module>
isinstance(names,list)
TypeError: isinstance() arg 2 must be a type or tuple of types
>>>
Am I missing something here?
Answers:
You’ve stomped on list
by assigning to a local variable of the same name. Don’t do that.
Apply this one :
if isinstance(names, type(list)):
But this works in Python (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32:
>>> names=['jill', 'jack']
>>> isinstance(names, list)
True
This can also happen if you’ve accidentally messed up the foreign key field syntax in your model. When writing a foreign key field, you can have:
ModelName
or:
'app_name.ModelName'
but you cannot have:
'ModelName'
Learnt that one the hard way.
If you are using Jupyter notebook, then do this:
del str
This situation happens when you refer to model which is not actual existed and you are referencing it in a fk relation with ‘app_name.model_name’ syntax.
The excerpt is, in lieu of app_name.model_name
in fk, first remove all those field with the syntax mentioned and apply migrations.
==> like the one below:
company = models.ForeignKey('core_company.Company',
null=True,
blank=True,
verbose_name="Company",
on_delete=models.CASCADE,related_name="%(app_label)s_%(class)s_related",
related_query_name="%(app_label)s_%(class)ss")
***** SOLUTION *****
Remove this field from the model and first make sure that core_company.Company
model actually exists. if it doesn’t first create migration for Company model and apply that migration. After that you can refer it like above.
>>> names=['jill','jack']
>>> isinstance(names,list)
Traceback (most recent call last):
File "<pyshell#291>", line 1, in <module>
isinstance(names,list)
TypeError: isinstance() arg 2 must be a type or tuple of types
>>>
Am I missing something here?
You’ve stomped on list
by assigning to a local variable of the same name. Don’t do that.
Apply this one :
if isinstance(names, type(list)):
But this works in Python (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32:
>>> names=['jill', 'jack']
>>> isinstance(names, list)
True
This can also happen if you’ve accidentally messed up the foreign key field syntax in your model. When writing a foreign key field, you can have:
ModelName
or:
'app_name.ModelName'
but you cannot have:
'ModelName'
Learnt that one the hard way.
If you are using Jupyter notebook, then do this:
del str
This situation happens when you refer to model which is not actual existed and you are referencing it in a fk relation with ‘app_name.model_name’ syntax.
The excerpt is, in lieu of app_name.model_name
in fk, first remove all those field with the syntax mentioned and apply migrations.
==> like the one below:
company = models.ForeignKey('core_company.Company',
null=True,
blank=True,
verbose_name="Company",
on_delete=models.CASCADE,related_name="%(app_label)s_%(class)s_related",
related_query_name="%(app_label)s_%(class)ss")
***** SOLUTION *****
Remove this field from the model and first make sure that core_company.Company
model actually exists. if it doesn’t first create migration for Company model and apply that migration. After that you can refer it like above.