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
This commit is contained in:
Luke Plant 2009-12-19 14:27:26 +00:00
parent f1ea26dd99
commit 01acd99947
7 changed files with 124 additions and 1 deletions

View File

@ -76,7 +76,17 @@ class Command(BaseCommand):
if has_bz2:
compression_types['bz2'] = bz2.BZ2File
app_fixtures = [os.path.join(os.path.dirname(app.__file__), 'fixtures') for app in get_apps()]
app_module_paths = []
for app in get_apps():
if hasattr(app, '__path__'):
# It's a 'models/' subpackage
for path in app.__path__:
app_module_paths.append(path)
else:
# It's a models.py module
app_module_paths.append(app.__file__)
app_fixtures = [os.path.join(os.path.dirname(path), 'fixtures') for path in app_module_paths]
for fixture_label in fixture_labels:
parts = fixture_label.split('.')

View File

@ -0,0 +1,2 @@

View File

@ -0,0 +1,18 @@
[
{
"pk": "2",
"model": "fixtures_model_package.article",
"fields": {
"headline": "Poker has no place on ESPN",
"pub_date": "2006-06-16 12:00:00"
}
},
{
"pk": "3",
"model": "fixtures_model_package.article",
"fields": {
"headline": "Time to reform copyright",
"pub_date": "2006-06-16 13:00:00"
}
}
]

View File

@ -0,0 +1,18 @@
[
{
"pk": "3",
"model": "fixtures_model_package.article",
"fields": {
"headline": "Copyright is fine the way it is",
"pub_date": "2006-06-16 14:00:00"
}
},
{
"pk": "4",
"model": "fixtures_model_package.article",
"fields": {
"headline": "Django conquers world!",
"pub_date": "2006-06-16 15:00:00"
}
}
]

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0">
<object pk="2" model="fixtures_model_package.article">
<field type="CharField" name="headline">Poker on TV is great!</field>
<field type="DateTimeField" name="pub_date">2006-06-16 11:00:00</field>
</object>
<object pk="5" model="fixtures_model_package.article">
<field type="CharField" name="headline">XML identified as leading cause of cancer</field>
<field type="DateTimeField" name="pub_date">2006-06-16 16:00:00</field>
</object>
</django-objects>

View File

@ -0,0 +1,10 @@
[
{
"pk": "1",
"model": "fixtures_model_package.article",
"fields": {
"headline": "Python program becomes self aware",
"pub_date": "2006-06-16 11:00:00"
}
}
]

View File

@ -0,0 +1,54 @@
from django.db import models
from django.conf import settings
class Article(models.Model):
headline = models.CharField(max_length=100, default='Default headline')
pub_date = models.DateTimeField()
def __unicode__(self):
return self.headline
class Meta:
app_label = 'fixtures_model_package'
ordering = ('-pub_date', 'headline')
__test__ = {'API_TESTS': """
>>> from django.core import management
>>> from django.db.models import get_app
# Reset the database representation of this app.
# This will return the database to a clean initial state.
>>> management.call_command('flush', verbosity=0, interactive=False)
# Syncdb introduces 1 initial data object from initial_data.json.
>>> Article.objects.all()
[<Article: Python program becomes self aware>]
# Load fixture 1. Single JSON file, with two objects.
>>> management.call_command('loaddata', 'fixture1.json', verbosity=0)
>>> Article.objects.all()
[<Article: Time to reform copyright>, <Article: Poker has no place on ESPN>, <Article: Python program becomes self aware>]
# Load fixture 2. JSON file imported by default. Overwrites some existing objects
>>> management.call_command('loaddata', 'fixture2.json', verbosity=0)
>>> 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>]
# Load a fixture that doesn't exist
>>> management.call_command('loaddata', 'unknown.json', verbosity=0)
# object list is unaffected
>>> 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>]
"""}
from django.test import TestCase
class SampleTestCase(TestCase):
fixtures = ['fixture1.json', 'fixture2.json']
def testClassFixtures(self):
"Check that test case has installed 4 fixture objects"
self.assertEqual(Article.objects.count(), 4)
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>]")