夹具未在 Django 中加载
Fixture not loading in Django
Settings.py:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FIXTURE_DIRS = (
os.path.join(BASE_DIR, '/deals/fixtures/'),
)
test_deals.py:
import unittest
from deals.models import Retailer
import django.test
class TestRetailer(django.test.TestCase):
def setUp(self):
fixtures = ['deals_test_data.json']
self.bestbuy = Retailer(Retailer.objects.get(pk=1))
def test_name(self):
self.assertEqual(self.bestbuy.name, 'Best Buy US')
项目结构:
project
- deals
- fixtures
- deals_test_data.json
- tests
- test_deals.py
错误:
Traceback (most recent call last):
File "/home/danny/PycharmProjects/askarby/deals/tests/test_deals.py", line 10, in setUp
self.bestbuy = Retailer(Retailer.objects.get(pk=1))
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/query.py", line 380, in get
self.model._meta.object_name
deals.models.DoesNotExist: Retailer matching query does not exist.
Destroying test database for alias 'default'...
Process finished with exit code 1
我试过不使用 FIXTURES_DIR 而是使用 fixtures=['../deals_test_data.jason']。我已经尝试删除 FIXTURES_DIR 中字符串前后的斜线。不开心。
如何让我的灯具加载?
装置正在加载,但我检索对象的代码一定已经关闭。这成功了:
import unittest
from django.test import TestCase
from deals.models import Retailer
class TestRetailer(TestCase):
fixtures = ['deals_test_data.json']
def test_loaded(self):
s = Retailer.objects.get(pk=1)
self.assertEquals(s.name, "Best Buy US")
Settings.py:
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FIXTURE_DIRS = (
os.path.join(BASE_DIR, '/deals/fixtures/'),
)
test_deals.py:
import unittest
from deals.models import Retailer
import django.test
class TestRetailer(django.test.TestCase):
def setUp(self):
fixtures = ['deals_test_data.json']
self.bestbuy = Retailer(Retailer.objects.get(pk=1))
def test_name(self):
self.assertEqual(self.bestbuy.name, 'Best Buy US')
项目结构:
project
- deals
- fixtures
- deals_test_data.json
- tests
- test_deals.py
错误:
Traceback (most recent call last):
File "/home/danny/PycharmProjects/askarby/deals/tests/test_deals.py", line 10, in setUp
self.bestbuy = Retailer(Retailer.objects.get(pk=1))
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/query.py", line 380, in get
self.model._meta.object_name
deals.models.DoesNotExist: Retailer matching query does not exist.
Destroying test database for alias 'default'...
Process finished with exit code 1
我试过不使用 FIXTURES_DIR 而是使用 fixtures=['../deals_test_data.jason']。我已经尝试删除 FIXTURES_DIR 中字符串前后的斜线。不开心。
如何让我的灯具加载?
装置正在加载,但我检索对象的代码一定已经关闭。这成功了:
import unittest
from django.test import TestCase
from deals.models import Retailer
class TestRetailer(TestCase):
fixtures = ['deals_test_data.json']
def test_loaded(self):
s = Retailer.objects.get(pk=1)
self.assertEquals(s.name, "Best Buy US")