2011-03-02 50 views
4

我在我的Django應用程序中使用燈具,但只有兩個應用程序正在加載它們的燈具。如何從所有應用程序加載Django燈具?

當我手動運行帶有--verbosity = 2的loaddata時,我可以看到它只在兩個應用程序中看起來,儘管我有更多與在裏面創建的fixtures目錄。

所有應用程序都已正確安裝在settings.py中。

從文檔看來,Django應該在每個安裝的應用程序的fixtures /目錄中進行搜索。

任何想法爲什麼一些應用程序被忽略?

+0

我確認存在此問題。使用Django 1.5.8進行測試,'python manage.py loaddata sample_content.json -v 2'顯示* loaddata *命令沒有看我已安裝的一些應用程序。 – Peterino 2014-06-27 19:12:09

回答

2

您必須將fixture數據放置在initial_data。[json | xml,...]文件中。

我認爲只有那些文件默認加載。

APPDIR /夾具/ initial_data.json

5

Initial_data獲取每個你執行syncdb時間進口。正如我記得的那樣,它也會覆蓋您手動完成的任何更改。

要加載其他燈具,您必須使用manage.py loaddata fixturename。 如果您在所有應用程序中使用通用命名機制,那麼效果會很好。 如果你不這樣做,你必須給loaddata每個人的名稱,或使用發現得到 燈具和exec loaddata的列表中他們每個人:

編輯:(我添加manage.py在virtualenv中/箱,當我安裝Django的包,我只用manage.py,如果你不這樣做,你將需要當然蟒蛇manage.py loaddata)

find . -name "*.json" -exec manage.py loaddata {} \; 

我用這個在fabfile自動化分期安裝:

def load_all_fixtures(): 
    """Loads all the fixtures in every dir""" 
    with cd(env.directory): 
     run(""" 
      source /usr/local/bin/virtualenvwrapper.sh && 
      workon %s && 
      find -L . -name "*.json" -exec manage.py loaddata {} \; 

      """ % env.virtualenv) 
3

試試這樣調用

python manage.py loaddata initial_data 

OR programmetically你可以這樣調用它

from django.core.management import call_command 
call_command('loaddata', 'initial_data', verbosity=3, database='default') 
2

的問題是,Django的只是會在提供一個模型應用燈具。你可能有一個沒有模型的應用程序,但你仍然想加載一些燈具(可能是另一個已安裝的應用程序的示例數據)。

這種現象在Django罪魁禍首是get_apps()loaddata.py

  1. django.core.management.commands.loaddata,線102
  2. django.db.models.loading,線132

欺騙的Django到看你的應用程序的<app>/fixtures/文件夾中必須添加(空)models.py文件到應用程序。 (保持良好,並對該文件發表評論以說明問題!)

<應用> /models.py

""" 
No real model, just an empty file to make Django load the fixtures. 
""" 

之後,運行python manage.py loaddata <fixture>手動會發現你的應用程序的固定文件。

+0

如果你在添加一個空模型之後得到一個帶有South遷移的'AttributeError',請檢查[Django South migration AttributeError](http://stackoverflow.com/questions/10826532/django-south-migration-attributeerror/24466401#24466401) – Peterino 2014-06-28 11:42:22