Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #6961 - loaddata fails if models is a package instead of a module

Thanks to pmd for report, zhaoz, mmalone and justinlilly for patch



git-svn-id: http://code.djangoproject.com/svn/django/trunk@11914 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 01acd99947b0723f5c9643aa4d438d553c82324c 1 parent f1ea26d
Luke Plant authored December 19, 2009
12  django/core/management/commands/loaddata.py
@@ -76,7 +76,17 @@ def read(self):
76 76
         if has_bz2:
77 77
             compression_types['bz2'] = bz2.BZ2File
78 78
 
79  
-        app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()]
  79
+        app_module_paths = []
  80
+        for app in get_apps():
  81
+            if hasattr(app, '__path__'):
  82
+                # It's a 'models/' subpackage
  83
+                for path in app.__path__:
  84
+                    app_module_paths.append(path)
  85
+            else:
  86
+                # It's a models.py module
  87
+                app_module_paths.append(app.__file__)
  88
+
  89
+        app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]
80 90
         for fixture_label in fixture_labels:
81 91
             parts = fixture_label.split('.')
82 92
 
2  tests/modeltests/fixtures_model_package/__init__.py
... ...
@@ -0,0 +1,2 @@
  1
+
  2
+
18  tests/modeltests/fixtures_model_package/fixtures/fixture1.json
... ...
@@ -0,0 +1,18 @@
  1
+[
  2
+    {
  3
+        "pk": "2", 
  4
+        "model": "fixtures_model_package.article", 
  5
+        "fields": {
  6
+            "headline": "Poker has no place on ESPN", 
  7
+            "pub_date": "2006-06-16 12:00:00"
  8
+        }
  9
+    }, 
  10
+    {
  11
+        "pk": "3", 
  12
+        "model": "fixtures_model_package.article", 
  13
+        "fields": {
  14
+            "headline": "Time to reform copyright", 
  15
+            "pub_date": "2006-06-16 13:00:00"
  16
+        }
  17
+    }
  18
+]
18  tests/modeltests/fixtures_model_package/fixtures/fixture2.json
... ...
@@ -0,0 +1,18 @@
  1
+[
  2
+    {
  3
+        "pk": "3", 
  4
+        "model": "fixtures_model_package.article", 
  5
+        "fields": {
  6
+            "headline": "Copyright is fine the way it is", 
  7
+            "pub_date": "2006-06-16 14:00:00"
  8
+        }
  9
+    }, 
  10
+    {
  11
+        "pk": "4", 
  12
+        "model": "fixtures_model_package.article", 
  13
+        "fields": {
  14
+            "headline": "Django conquers world!", 
  15
+            "pub_date": "2006-06-16 15:00:00"
  16
+        }
  17
+    }
  18
+]
11  tests/modeltests/fixtures_model_package/fixtures/fixture2.xml
... ...
@@ -0,0 +1,11 @@
  1
+<?xml version="1.0" encoding="utf-8"?>
  2
+<django-objects version="1.0">
  3
+    <object pk="2" model="fixtures_model_package.article">
  4
+        <field type="CharField" name="headline">Poker on TV is great!</field>
  5
+        <field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field>
  6
+    </object>
  7
+    <object pk="5" model="fixtures_model_package.article">
  8
+        <field type="CharField" name="headline">XML identified as leading cause of cancer</field>
  9
+        <field type="DateTimeField" name="pub_date">2006-06-16 16:00:00</field>
  10
+    </object>
  11
+</django-objects>
10  tests/modeltests/fixtures_model_package/fixtures/initial_data.json
... ...
@@ -0,0 +1,10 @@
  1
+[
  2
+    {
  3
+        "pk": "1", 
  4
+        "model": "fixtures_model_package.article", 
  5
+        "fields": {
  6
+            "headline": "Python program becomes self aware", 
  7
+            "pub_date": "2006-06-16 11:00:00"
  8
+        }
  9
+    }
  10
+]
54  tests/modeltests/fixtures_model_package/models/__init__.py
... ...
@@ -0,0 +1,54 @@
  1
+from django.db import models
  2
+from django.conf import settings
  3
+
  4
+class Article(models.Model):
  5
+    headline = models.CharField(max_length=100, default='Default headline')
  6
+    pub_date = models.DateTimeField()
  7
+
  8
+    def __unicode__(self):
  9
+        return self.headline
  10
+
  11
+    class Meta:
  12
+        app_label = 'fixtures_model_package'
  13
+        ordering = ('-pub_date', 'headline')
  14
+
  15
+__test__ = {'API_TESTS': """
  16
+>>> from django.core import management
  17
+>>> from django.db.models import get_app
  18
+
  19
+# Reset the database representation of this app.
  20
+# This will return the database to a clean initial state.
  21
+>>> management.call_command('flush', verbosity=0, interactive=False)
  22
+
  23
+# Syncdb introduces 1 initial data object from initial_data.json.
  24
+>>> Article.objects.all()
  25
+[<Article: Python program becomes self aware>]
  26
+
  27
+# Load fixture 1. Single JSON file, with two objects.
  28
+>>> management.call_command('loaddata', 'fixture1.json', verbosity=0)
  29
+>>> Article.objects.all()
  30
+[<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  31
+
  32
+# Load fixture 2. JSON file imported by default. Overwrites some existing objects
  33
+>>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
  34
+>>> Article.objects.all()
  35
+[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  36
+
  37
+# Load a fixture that doesn't exist
  38
+>>> management.call_command('loaddata', 'unknown.json', verbosity=0)
  39
+
  40
+# object list is unaffected
  41
+>>> Article.objects.all()
  42
+[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
  43
+"""}
  44
+
  45
+
  46
+from django.test import TestCase
  47
+
  48
+class SampleTestCase(TestCase):
  49
+    fixtures = ['fixture1.json', 'fixture2.json']
  50
+
  51
+    def testClassFixtures(self):
  52
+        "Check that test case has installed 4 fixture objects"
  53
+        self.assertEqual(Article.objects.count(), 4)
  54
+        self.assertEquals(str(Article.objects.all()), "[<Article: Django conquers world!>, <Article: Copyright is fine the way it is>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]")

0 notes on commit 01acd99

Please sign in to comment.
Something went wrong with that request. Please try again.