Django tests - fixture User matching query does not exist
Question: I'm trying to run a test that loads a fixture. One the models has
`GenericForeign` key to `ContentType` and a Foreign key to `auth.Users`. It
associates users with content they create. I created fixture with `--natural`
key (as per below) and can foreign keys resolved to names.

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

When running my tests I get the following error:

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

Sample database object as dumped by 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"
    }
    },
    

Full traceback:

    
    
        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)
    

EDIT:

I have confirmed that all users referenced by vendor model exist in the db.

UPDATE 1:

My prpject uses GenericForeign key to ContentType and a Foreign key to
auth.Users. I dumped the data using the --natural option but this lead to the
problem described above. Now I removed the --natural option and instead dumped
data for all 3 apps myApp, auth, contenttypes. When I'm running the test I get
"Could not load contenttypes.ContentType(pk=50): columns app_label, model are
not unique". I think this is due to the contenttypes being created dynamically
when models are imported. What's the way around this?


Answer: They may exists in "the db". But do they exist in your test database? When you
run tests, Django creates a [test
database](https://docs.djangoproject.com/en/dev/topics/testing/overview/#the-
test-database). So you'll have to dump the Users from you db and load them as
fixtures too.

Fixtures are a nightmare to maintain. I advise you to use something like
[model mommy](https://github.com/vandersonmota/model_mommy) or [factory
boy](https://github.com/rbarrois/factory_boy) to create your fixtures at test
time. Personally I like the model mommy API best, but your taste may differ.

