Opened 7 years ago

Last modified 7 years ago

#28972 closed Bug

`RelatedObjectDoesNotExist` during `loaddata` of models with multi-table inheritance — at Version 1

Reported by: Kal Sze Owned by: nobody
Component: Core (Serialization) Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Kal Sze)

Software versions

  • Python 3.6.3
  • Django 2.0
  • django-model-utils 3.1.1 (from git)
  • PostgreSQL 9.4
  • psycopg2 2.7.3.2

I have a licenses app, with these models:

  • BaseLicense, a concrete model, with a ForeignKey to settings.AUTH_USER_MODEL.
  • CorelDrawLicense, which inherits from BaseLicense through multi-table inheritance
  • AdobePhotoshopLicense, which inherits from BaseLicense through multi-table inheritance

I created an instance of AdobePhotoshopLicense, then tried to do manage.py dumpdata and manage.py loaddata.

dumpdata "worked" - there was no error.

However, loaddata failed with this traceback:

Traceback (most recent call last):
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/serializers/json.py", line 69, in Deserializer
    yield from PythonDeserializer(objects, **options)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/serializers/python.py", line 170, in Deserializer
    obj = base.build_instance(Model, data, using)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/serializers/base.py", line 225, in build_instance
    natural_key = obj.natural_key()
  File "/home/kal/git/nk_test/licenses/models.py", line 33, in natural_key
    return (self.user.natural_key(), self.license_key)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 175, in __get__
    "%s has no %s." % (self.field.model.__name__, self.field.name)
django.db.models.fields.related_descriptors.RelatedObjectDoesNotExist: BaseLicense has no user.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "./manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 72, in handle
    self.loaddata(fixture_labels)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 113, in loaddata
    self.load_label(fixture_label)
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/management/commands/loaddata.py", line 168, in load_label
    for obj in objects:
  File "/home/kal/.virtualenvs/nk_test-5iWsqJVv/lib/python3.6/site-packages/django/core/serializers/json.py", line 73, in Deserializer
    raise DeserializationError() from exc
django.core.serializers.base.DeserializationError: Problem installing fixture '/home/kal/git/nk_test/./fixtures/dumpdata.json':

Here are the exact steps:

  1. Create model instances in management shell
    >>> from django.contrib.auth import get_user_model
    >>> User = get_user_model()
    >>> kal = User.objects.create_user('kal', 'kal@foobar.com', 'qwer1234')
    >>> from licenses.models import AdobePhotoshopLicense
    >>> l = AdobePhotoshopLicense.objects.create(user=kal, license_key='12345')
    
  1. Dump the data into a fixtures json
    $ python manage.py  dumpdata -o fixtures.json --format json --natural-foreign --natural-primary auth licenses
    
  1. Edit settings.py to switch to an empty database
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            #'NAME': 'nk_test',
            'NAME': 'nk_test_2',
            'USER': 'kal',
            'HOST': '',
            'PORT': 5432,
        }
    }
    
  1. Run migrations on the new empty database
    $ python manage.py migrate
    
  1. Try to load the data from fixtures.json
    $ python manage.py loaddata --format json fixtures.json
    

I have attached the models.py and fixtures.json file to the ticket.

Also note that this bug doesn't have to do with my use of the custom InheritanceManager. I could let the BaseLicense model use the default manager and still get the same bug.

Change History (3)

by Kal Sze, 7 years ago

Attachment: models.py added

models.py

by Kal Sze, 7 years ago

Attachment: fixtures.json added

fixtures.json, reformatted for readability

comment:1 by Kal Sze, 7 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top