Django

Code

Ticket #6961 (closed: fixed)

Opened 2 years ago

Last modified 3 months ago

loaddata fails if models directory instead of models.py

Reported by: pmd Assigned to: nobody
Milestone: Component: django-admin.py
Version: SVN Keywords: loaddata
Cc: zilingzhao@gmail.com, mjmalone@gmail.com Triage Stage: Ready for checkin
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

If the models for an app have been split into multiple files within a models subdirectory of the app then loaddata looks for fixtures in appname/models/fixtures instead of appname/fixtures.

To reproduce:

   django-admin.py startproject proj1
   cd proj1
   django-admin.py startapp foo
   cd foo
   vi ../settings.py  # set INSTALLED_APPS to just proj1.foo
   mkdir fixtures
   touch fixtures/abc.json

   ../manage.py loaddata --verbosity=2 abc.json
   Problem installing fixture '/data/home/leotest/projects/proj1/../proj1/foo/fixtures/abc.json': No JSON object could be decoded
   # location is correct, error is because file is empty

   rm models.py
   mkdir models
   touch models/__init__.py
   ../manage.py loaddata --verbosity=2 abc.json

   Loading 'abc' fixtures...
   Checking '/data/home/leotest/projects/proj1/../proj1/foo/models/fixtures' for fixtures...
   Trying '/data/home/leotest/projects/proj1/../proj1/foo/models/fixtures' for json fixture      'abc'...
   No json fixture 'abc' in '/data/home/leotest/projects/proj1/../proj1/foo/models/fixtures'.
   Checking absolute path for fixtures...
   Trying absolute path for json fixture 'abc'...
   No json fixture 'abc' in absolute path.
   No fixtures found.

The problem is that loaddata uses app.__file__ and app object is actually the app.models module rather than the app module. This is around line 141 in core/management/commands/loaddata.py.

Note that setting app_label in Meta subclass for each model has no bearing on this problem.

Attachments

patch1.txt (0.8 kB) - added by pmd on 04/07/08 08:15:51.
patch to make loaddata use app dir and not models module dir
model_folder_fixture_load_test.patch (1.6 kB) - added by zhaoz on 10/10/08 16:22:28.
needs init.py file in loaddata folder. Tests fixture loading with models folder as opposed to models.py
patch2.txt (0.9 kB) - added by mmalone on 01/30/09 16:08:52.
Patch that checks whether models module is a package by checking for path attribute
model_folder_fixture_load_test_for_patch2.patch (5.7 kB) - added by mmalone on 01/30/09 16:12:04.
Tests for patch 2
patch3.txt (0.7 kB) - added by HuCy on 06/17/09 10:27:50.
Another approach. Found this Ticket too late, so i did a patch by myself.
patch_3_tests_combined.diff (6.6 kB) - added by justinlilly on 11/07/09 12:40:16.
patch 3 combined with tests.

Change History

04/07/08 08:15:51 changed by pmd

  • attachment patch1.txt added.

patch to make loaddata use app dir and not models module dir

04/07/08 08:19:17 changed by pmd

  • needs_better_patch changed.
  • has_patch set to 1.
  • needs_tests changed.
  • needs_docs changed.

The attached patch (patch1) is against r7403 and tested on Linux. Not sure if it would work on windows.

06/14/08 10:53:24 changed by programmerq

  • stage changed from Unreviewed to Accepted.

This definitely seems like a bug.

10/03/08 18:03:55 changed by zhaoz

  • component changed from Core framework to django-admin.py.

What is the status of this bug? I recently bumped into this issue, it looks like it hasn't been touched for quite awhile.

What needs to be done?

10/06/08 19:23:21 changed by anonymous

  • cc set to zilingzhao@gmail.com.

10/10/08 16:22:28 changed by zhaoz

  • attachment model_folder_fixture_load_test.patch added.

needs init.py file in loaddata folder. Tests fixture loading with models folder as opposed to models.py

(follow-up: ↓ 6 ) 10/10/08 16:26:37 changed by zhaoz

  • needs_better_patch set to 1.

The patch did not work for me when patched against svn trunk's 9222, failed test.

(in reply to: ↑ 5 ) 10/10/08 16:35:13 changed by zhaoz

  • needs_better_patch deleted.

Replying to zhaoz:

The patch did not work for me when patched against svn trunk's 9222, failed test.

Scratch what I last said, it's working, but it doesn't seem to be done in a very elegant way.

01/30/09 16:07:05 changed by mmalone

  • cc changed from zilingzhao@gmail.com to zilingzhao@gmail.com, mjmalone@gmail.com.

Seems what we're really trying to do here is determine whether the models module (return by get_apps()) is a package (a directory with an __init__.py and submodules) or a normal module. According to an essay that was published when package support landed in Python, the only difference between a package and an ordinary module is that a package has a __path__ attribute. Handily enough, the __path__ attribute is an array that, by default, contains a single string pointing to the package directory, which is at the equivalent level in the file system directory hierarchy as models.py.

I've attached a patch that checks the modules for the existence of a __path__ attribute and handles them properly (and another patch containing some tests). I'm fairly confident this is the right way to do this, given the essay linked to above. I think this method may also be used to remove the Meta.app_label requirement for models submodules.

01/30/09 16:08:52 changed by mmalone

  • attachment patch2.txt added.

Patch that checks whether models module is a package by checking for path attribute

01/30/09 16:12:04 changed by mmalone

  • attachment model_folder_fixture_load_test_for_patch2.patch added.

Tests for patch 2

06/17/09 10:27:50 changed by HuCy

  • attachment patch3.txt added.

Another approach. Found this Ticket too late, so i did a patch by myself.

06/23/09 05:19:45 changed by stephaner

I'm not sure if it's related to this bug but the loaddata command works only if there's a models.py file (empty) in the app directory:

That doesn't work:

app/__init__.py
    fixtures/data.json

that works:

app/__init__.py
    models.py
    fixtures/data.json

and the documentation says (or I didn't find) nothing about that requirement. Tested with trunk r11032.

11/07/09 12:39:42 changed by justinlilly

  • stage changed from Accepted to Ready for checkin.

Patch looks good, applied cleanly. I've combined the tests for patch3 and patch3 into this patch.

11/07/09 12:40:16 changed by justinlilly

  • attachment patch_3_tests_combined.diff added.

patch 3 combined with tests.

12/19/09 08:27:26 changed by lukeplant

  • status changed from new to closed.
  • resolution set to fixed.

(In [11914]) Fixed #6961 - loaddata fails if models is a package instead of a module

Thanks to pmd for report, zhaoz, mmalone and justinlilly for patch

12/19/09 09:27:50 changed by lukeplant

(In [11918]) [1.1.X] Fixed #6961 - loaddata fails if models is a package instead of a module Thanks to pmd for report, zhaoz, mmalone and justinlilly for patch

Backport of 11914 from trunk.


Add/Change #6961 (loaddata fails if models directory instead of models.py)




Change Properties
Action