RubyNoob RubyNoob - 3 years ago 235
Python Question

Fixture not loading in Django

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__)))

os.path.join(BASE_DIR, '/deals/fixtures/'),

import unittest
from deals.models import Retailer
import django.test

class TestRetailer(django.test.TestCase):

def setUp(self):
fixtures = ['deals_test_data.json'] = Retailer(Retailer.objects.get(pk=1))

def test_name(self):
self.assertEqual(, 'Best Buy US')

Project structure:

- deals
- fixtures
- deals_test_data.json
- tests


Traceback (most recent call last):
File "/home/danny/PycharmProjects/askarby/deals/tests/", line 10, in setUp = Retailer(Retailer.objects.get(pk=1))
File "/home/danny/.virtualenvs/AskArby/lib/python3.5/site-packages/django/db/models/", 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/", line 380, in get
deals.models.DoesNotExist: Retailer matching query does not exist.

Destroying test database for alias 'default'...

Process finished with exit code 1

I've tried not using FIXTURES_DIR but instead using fixtures=['../deals_test_data.jason']. I've tried removing the slashes at the front and back of the string in my FIXTURES_DIR. No joy.

How can I make my fixtures load?

Answer Source

The fixtures were loading, but my code to retrieve the object must have been off. This nailed it:

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(, "Best Buy US")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download