在Windows上的Docker中的django中运行测试期间,如何修复'TypeError:isinstance()arg 2必须是类型或类型元组'

时间:2019-06-05 13:39:18

标签: python django windows docker

我最近搬到了docker。我的应用程序现在正在运行。我在我的项目中运行python manage.py测试。但是我遇到了很多错误“ TypeError:isinstance()arg 2必须是类型或类型的元组”。

相同的设置在Linux机器上也可以使用。现在我正在使用Windows 10 one。 主要docker映像是ubuntu14.04,使用python版本3.5.2,这是我的pip冻结:

amqp==2.5.0
atomicwrites==1.3.0
attrs==19.1.0
Babel==2.7.0
backcall==0.1.0
billiard==3.5.0.2
celery==4.1.0
decorator==4.4.0
dictdiffer==0.5.0.post1
dicttoxml==1.6.6
Django==2.0.2
django-common-helpers==0.9.2
django-cron==0.5.0
django-model-utils==3.0.0
django-params==0.3
django-pipeline==1.6.8
django-pytest==0.2.0
django-ranged-response==0.2.0
django-redis==4.8.0
django-simple-captcha==0.5.9
django-widget-tweaks==1.4.1
djangorestframework==3.7.7
et-xmlfile==1.0.1
factory-boy==2.8.1
Faker==0.7.7
fakeredis==0.8.2
flower==0.9.2
importlib-metadata==0.17
ipdb==0.12
ipython==7.5.0
ipython-genutils==0.2.0
jdcal==1.4.1
jedi==0.13.3
Jinja2==2.10.1
JPype1==0.6.1
kombu==4.1.0
lxml==3.6.0
MarkupSafe==1.1.1
more-itertools==7.0.0
openpyxl==2.4.1
packaging==19.0
parso==0.4.0
pathlib2==2.3.3
pexpect==4.7.0
pickleshare==0.7.5
Pillow==2.7.0
pluggy==0.12.0
prompt-toolkit==2.0.9
psycopg2==2.7.5
ptyprocess==0.6.0
py==1.8.0
Pygments==2.4.2
pyparsing==2.4.0
pytest==4.6.2
pytest-django==3.5.0
python-dateutil==2.8.0
pytz==2016.3
redis==2.10.6
reportlab==3.3.0
six==1.12.0
sqlparse==0.1.0
tornado==5.1.1
traitlets==4.3.2
vine==1.3.0
wcwidth==0.1.7
xlwt==1.1.2
zipp==0.5.1

self = <django.db.models.fields.DateTimeField: created_at>, value = datetime.datetime(2019, 6, 5, 16, 23, 2, 463090)

    def to_python(self, value):
        if value is None:
            return value
>       if isinstance(value, datetime.datetime):
E       TypeError: isinstance() arg 2 must be a type or tuple of types

/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py:1353: TypeError

这是完整的踪迹

Traceback (most recent call last):
  File "/code/app/app/test_api.py", line 19, in setUp
    self.user= UserFactory(role=User.ROLE_ADMIN, is_superuser=True)
  File "/usr/local/lib/python3.5/dist-packages/factory/base.py", line 69, in __call__
    return cls.create(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/factory/base.py", line 622, in create
    attrs = cls.attributes(create=True, extra=kwargs)
  File "/usr/local/lib/python3.5/dist-packages/factory/base.py", line 453, in attributes
    force_sequence=force_sequence,
  File "/usr/local/lib/python3.5/dist-packages/factory/containers.py", line 324, in build
    return stub.__fill__()
  File "/usr/local/lib/python3.5/dist-packages/factory/containers.py", line 83, in __fill__
    res[attr] = getattr(self, attr)
  File "/usr/local/lib/python3.5/dist-packages/factory/containers.py", line 108, in __getattr__
    val = val.evaluate(self, self.__containers)
  File "/usr/local/lib/python3.5/dist-packages/factory/containers.py", line 227, in evaluate
    containers=containers,
  File "/usr/local/lib/python3.5/dist-packages/factory/declarations.py", line 324, in evaluate
    return self.generate(sequence, obj, create, defaults)
  File "/usr/local/lib/python3.5/dist-packages/factory/declarations.py", line 412, in generate
    return subfactory.simple_generate(create, **params)
  File "/usr/local/lib/python3.5/dist-packages/factory/base.py", line 709, in simple_generate
    return cls.generate(strategy, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/factory/base.py", line 676, in generate
    return action(**kwargs)
  File "/usr/local/lib/python3.5/dist-packages/factory/base.py", line 623, in create
    return cls._generate(True, attrs)
  File "/usr/local/lib/python3.5/dist-packages/factory/base.py", line 548, in _generate
    obj = cls._prepare(create, **attrs)
  File "/usr/local/lib/python3.5/dist-packages/factory/base.py", line 523, in _prepare
    return cls._create(model_class, *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/factory/django.py", line 181, in _create
    return manager.create(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 417, in create
    obj.save(force_insert=True, using=self.db)
  File "/code/clinica/audit_track/models.py", line 32, in save
    super(UpdatedAtMixin, self).save(*args, **kwargs)
  File "/code/clinica/audit_track/models.py", line 98, in save
    super(CreatedByMixin, self).save(*args, **kwargs)
  File "/code/clinica/audit_track/models.py", line 116, in save
    super(UpdatedByMixin, self).save(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 759, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 842, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/base.py", line 880, in _do_insert
    using=using, raw=raw)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 1125, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1280, in execute_sql
    for sql, params in self.as_sql():
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1233, in as_sql
    for obj in self.query.objs
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1233, in <listcomp>
    for obj in self.query.objs
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1232, in <listcomp>
    [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields]
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py", line 1172, in prepare_value
    value = field.get_db_prep_save(value, connection=self.connection)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 767, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1431, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1410, in get_prep_value
    value = super().get_prep_value(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1271, in get_prep_value
    return self.to_python(value)
  File "/usr/local/lib/python3.5/dist-packages/django/db/models/fields/__init__.py", line 1353, in to_python
    if isinstance(value, datetime.datetime):
TypeError: isinstance() arg 2 must be a type or tuple of types

2 个答案:

答案 0 :(得分:0)

在我的情况下。我已经测试过手动模拟datetime的地方。 如果测试失败,则模拟无法正确关闭。 我的建议是,运行测试时不要限制终端的滚动缓冲区,并从头开始研究失败的测试。

答案 1 :(得分:0)

对我来说,解决方案取决于我的导入方式

from datetime import datetime, date
print(isinstance( datetime.fromtimestamp(0), date ))
#returns True

如果按照上述datetime.date的方式导入,则在此处将无法识别。

import datetime
print(isinstance( datetime.datetime.fromtimestamp(0), datetime.date ))
#returns True

如果按照上述date的方式导入,则在此处将无法识别。