运行某些单元测试时,出现DeserializationError
错误。同一文件中的其他测试类正确使用相同的夹具。我很困惑,想知道我做错了什么。
以下是代码:
class CreateViewTest(TransactionTestCase):
fixtures = ['data.yaml']
def test_create_post_passed(self):
current = len(Timesheet.objects.all())
data = {'start': datetime.date.today(), }
response = self.client.post('/timetracker/create/', data)
self.assertEqual(response.status_code, 200, response)
self.assertEqual(current + 1, len(Timesheet.objects.all()))
这是运行单元测试的输出:
======================================================================
ERROR: test_create_post_passed (timetracker.tests.CreateViewTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/test/testcases.py", line 182, in __call__
self._pre_setup()
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/test/testcases.py", line 787, in _pre_setup
self._fixture_setup()
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/test/testcases.py", line 840, in _fixture_setup
**{'verbosity': 0, 'database': db_name})
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/management/__init__.py", line 120, in call_command
return command.execute(*args, **defaults)
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/management/base.py", line 445, in execute
output = self.handle(*args, **options)
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 60, in handle
self.loaddata(fixture_labels)
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 90, in loaddata
self.load_label(fixture_label)
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 141, in load_label
for obj in objects:
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/serializers/pyyaml.py", line 79, in Deserializer
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/serializers/pyyaml.py", line 73, in Deserializer
for obj in PythonDeserializer(yaml.load(stream, Loader=SafeLoader), **options):
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/serializers/python.py", line 132, in Deserializer
m2m_data[field.name] = [m2m_convert(pk) for pk in field_value]
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/core/serializers/python.py", line 127, in m2m_convert
return field.rel.to._default_manager.db_manager(db).get_by_natural_key(*value).pk
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/contrib/auth/models.py", line 36, in get_by_natural_key
content_type=ContentType.objects.db_manager(self.db).get_by_natural_key(app_label, model),
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/db/models/manager.py", line 127, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/usr/Envs/intranet/lib/python2.7/site-packages/django/db/models/query.py", line 334, in get
self.model._meta.object_name
DeserializationError: Problem installing fixture '/home/usr/repos/intranet/intranet_site/timetracker/fixtures/users.yaml': Permission matching query does not exist.
答案 0 :(得分:1)
转储身份验证权限表后,我遇到了同样的问题。删除一些权限后问题已经消失。以下是已删除的权限列表:
- fields:
codename: add_site
content_type: [sites, site]
name: Can add site
model: auth.permission
- fields:
codename: change_site
content_type: [sites, site]
name: Can change site
model: auth.permission
- fields:
codename: delete_site
content_type: [sites, site]
name: Can delete site
model: auth.permission
- fields:
codename: add_migrationhistory
content_type: [south, migrationhistory]
name: Can add migration history
model: auth.permission
- fields:
codename: change_migrationhistory
content_type: [south, migrationhistory]
name: Can change migration history
model: auth.permission
- fields:
codename: delete_migrationhistory
content_type: [south, migrationhistory]
name: Can delete migration history
model: auth.permission
事实证明,我的项目中未安装sites
和south
个应用,因此所有这些权限都与非现有应用相关。这些应用程序之前已安装,并在Django升级后被删除。但存储在数据库中的权限保持不变