Opened 17 years ago
Closed 14 years ago
#6961 closed (fixed)
loaddata fails if models directory instead of models.py
Reported by: | Phil Davis | Owned by: | nobody |
---|---|---|---|
Component: | Core (Management commands) | Version: | dev |
Severity: | Keywords: | loaddata | |
Cc: | zilingzhao@…, mjmalone@… | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
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 (6)
Change History (19)
by , 17 years ago
Attachment: | patch1.txt added |
---|
comment:1 by , 17 years ago
Has patch: | set |
---|
The attached patch (patch1) is against r7403 and tested on Linux. Not sure if it would work on windows.
comment:3 by , 16 years ago
Component: | Core framework → 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?
comment:4 by , 16 years ago
Cc: | added |
---|
by , 16 years ago
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 comment:5 by , 16 years ago
Patch needs improvement: | set |
---|
The patch did not work for me when patched against svn trunk's 9222, failed test.
comment:6 by , 16 years ago
Patch needs improvement: | unset |
---|
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.
comment:7 by , 16 years ago
Cc: | added |
---|
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.
by , 16 years ago
Attachment: | patch2.txt added |
---|
Patch that checks whether models module is a package by checking for path attribute
by , 16 years ago
Attachment: | model_folder_fixture_load_test_for_patch2.patch added |
---|
Tests for patch 2
by , 15 years ago
Attachment: | patch3.txt added |
---|
Another approach. Found this Ticket too late, so i did a patch by myself.
comment:8 by , 15 years ago
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.
comment:9 by , 15 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Patch looks good, applied cleanly. I've combined the tests for patch3 and patch3 into this patch.
comment:10 by , 15 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:11 by , 15 years ago
comment:12 by , 14 years ago
Resolution: | fixed |
---|---|
Status: | closed → reopened |
Bump on stephaner's comment. Why are models needed at all? Consider the case where you have an app for doing various things for your site. There may not be any models, but there may be an initial_data fixture to add in a default user or Site information.
comment:13 by , 14 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Requiring models for various things is a seperate issue, closing this as the bug here was fixed.
patch to make loaddata use app dir and not models module dir