Django:如何迭代一个在用sqlite测试时不会失败的选择字段的记录集?



我正在尝试使用内存中的SQLITE数据库(Django 1.6)进行测试:

[设置/test.py]:

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.sqlite3",
        "NAME": ":memory:",
        "USER": "",
        "PASSWORD": "",
        "HOST": "",
        "PORT": "",
    },
}

我需要将以下内容传递到选择字段

def VERSIONS():
    v_win_car = Version.objects.filter(
        platform='WIN',
        section='CAR',
        language='EN').order_by('-official_date')[:5]
    VCAR = [[b.version + '-' + b.section, b.version + '-' + b.section] for b in
            v_win_car]
    v_win_bike = Version.objects.filter(
        platform='WIN',
        section='BIKE',
        language='EN').order_by('-official_date')[:5]
    VBIKE = [[b.version + '-' + b.section, b.version + '-' + b.section] for b in
             v_win_bike]
    return VCAR + VBIKE

(现在,当我试图解决这个问题时,它已包含在功能中,一开始就是 版本= vcar vbike)

形式:

version = forms.ChoiceField(choices=VERSIONS())

如果我使用本地设置和MySQL数据库运行测试,则不会抱怨,但是当我将测试设置与SQLITE数据库一起使用时,它会失败(还使用SQLITE数据库文件test.db):

    Creating test database for alias 'default'...
E
======================================================================
ERROR: control.tests.test_views (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: control.tests.test_views
Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/loader.py", line 252, in _find_tests
    module = self._get_module_from_name(name)
  File "/usr/lib/python2.7/unittest/loader.py", line 230, in _get_module_from_name
    __import__(name)
  File "/vagrant_data/projects/compdealer16/compdealer/control/tests/test_views.py", line 7, in <module>
    from control.views import VistaJsonGetNextVersion, crear_VERSIONS
  File "/vagrant_data/projects/compdealer16/compdealer/control/views.py", line 25, in <module>
    from .forms import RenoveForm, AddEquipmentForm, AddSoftwareForm, QueryForm
  File "/vagrant_data/projects/compdealer16/compdealer/control/forms.py", line 309, in <module>
    class AddEquipmentForm(forms.Form):
  File "/vagrant_data/projects/compdealer16/compdealer/control/forms.py", line 338, in AddEquipmentForm
    version = forms.ChoiceField(choices=VERSIONS())
  File "/vagrant_data/projects/compdealer16/compdealer/control/forms.py", line 57, in VERSIONS
    v_win_car]
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/models/query.py", line 96, in __iter__
    self._fetch_all()
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/models/query.py", line 220, in iterator
    for row in compiler.results_iter():
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 710, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 781, in execute_sql
    cursor.execute(sql, params)
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/utils.py", line 99, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/backends/util.py", line 53, in execute
    return self.cursor.execute(sql, params)
  File "/home/vagrant/.virtualenvs/dealerRC/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 450, in execute
    return Database.Cursor.execute(self, query, params)
OperationalError: no such table: versiones_version

----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
Destroying test database for alias 'default'...

据我了解,它试图在未创建表时在导入时间评估记录集。

有什么方法可以解决此问题?

(如果您为这个问题找到更好的标题...)

选项1:使用ModelChoiceField:

version = forms.ModelChoiceField(queryset=Version.objects.filter(...))

选项2:创建表单(或在其构造函数中)之后立即更改选择:

form = MyForm()
form.fields['version'].choices = ((1, 'One'), (2, 'Two'))

(每次创建表格时,都可以认为您的数据库更好)

最新更新