Django测试 - 夹具用户匹配查询不存在

时间:2014-03-20 15:19:11

标签: django django-testing django-fixtures django-serializer

我正在尝试运行加载夹具的测试。其中一个模型具有GenericForeign ContentType的密钥和auth.Users的外键。它将用户与他们创建的内容相关联。我使用--natural键创建了fixture(如下所示),外键可以解析为名称。

python manage.py dumpdata mtm --natural --indent=4

运行我的测试时出现以下错误:

DeserializationError: Problem installing fixture 'fix.json': User matching query does not  exist.

manage.py:

转储的示例数据库对象
{
"pk": 7, 
"model": "xx.vendor", 
"fields": {
    "phone_number": "777777777777777", 
    "alternative_phone_number": "", 
    "object_id": 1, 
    "contact_email": "", 
    "user": [
        "john"
    ], 
    "content_type": [
        "xx", 
        "axe"
    ], 
    "contact_person": "jimmy"
}
},

完整追溯:

    Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/test/testcases.py", line 178, in __call__
    self._pre_setup()
  File "/usr/local/lib/python2.7/dist-packages/django/test/testcases.py", line 749, in _pre_setup
    self._fixture_setup()
  File "/usr/local/lib/python2.7/dist-packages/django/test/testcases.py", line 881, in _fixture_setup
    'skip_validation': True,
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 159, in call_command
    return klass.execute(*args, **defaults)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/loaddata.py", line 55, in handle
    self.loaddata(fixture_labels)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/loaddata.py", line 84, in loaddata
    self.load_label(fixture_label)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/loaddata.py", line 134, in load_label
    for obj in objects:
  File "/usr/local/lib/python2.7/dist-packages/django/core/serializers/json.py", line 76, in Deserializer
    six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])
  File "/usr/local/lib/python2.7/dist-packages/django/core/serializers/json.py", line 70, in Deserializer
    for obj in PythonDeserializer(objects, **options):
  File "/usr/local/lib/python2.7/dist-packages/django/core/serializers/python.py", line 124, in Deserializer
    obj = field.rel.to._default_manager.db_manager(db).get_by_natural_key(*field_value)
  File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/models.py", line 167, in get_by_natural_key
    return self.get(**{self.model.USERNAME_FIELD: username})
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 307, in get
    self.model._meta.object_name)

编辑:

我已确认供应商模型引用的所有用户都存在于db。

更新1:

我的prpject使用GenericForeign键作为ContentType,使用外键作为auth.Users。我使用--natural选项转储数据,但这会导致上述问题。现在我删除了--natural选项,而是转储了所有3个应用程序myApp,auth,contenttypes的数据。当我运行测试时,我得到“无法加载contenttypes.ContentType(pk = 50):列app_label,模型不是唯一的”。我认为这是由于在导入模型时动态创建了contenttypes。这是怎么回事?

1 个答案:

答案 0 :(得分:2)

它们可能存在于“db”中。但它们是否存在于您的测试数据库中?当您运行测试时,Django会创建一个test database。所以你必须从你的数据库中转储用户并将它们作为固定装置加载。

固定装置是一个难以维持的噩梦。我建议你使用像model mommyfactory boy这样的东西在测试时创建你的灯具。就个人而言,我最喜欢模特妈咪API,但你的口味可能会有所不同。