# HG changeset patch
# User Federico Heinz <fheinz@vialibre.org.ar>
# Date 1289675887 10800
# Node ID 322d8f403154422a4f6b3e3dccf2da011ad70fa0
# Parent c8000aad4c103d7c18a36f6f7c190212227c46b2
Fixed custom SQL when models resides in a directory, not a file
diff -r c8000aad4c10 -r 322d8f403154 django/core/management/sql.py
|
a
|
b
|
|
| 141 | 141 | |
| 142 | 142 | def custom_sql_for_model(model, style, connection): |
| 143 | 143 | opts = model._meta |
| 144 | | app_dir = os.path.normpath(os.path.join(os.path.dirname(models.get_app(model._meta.app_label).__file__), 'sql')) |
| | 144 | app = models.get_app(model._meta.app_label) |
| | 145 | if hasattr(app, '__path__'): |
| | 146 | app_dir = os.path.dirname(app.__path__[0]) |
| | 147 | else: |
| | 148 | app_dir = os.path.dirname(app.__file__) |
| | 149 | app_dir = os.path.normpath(os.path.join(app_dir, 'sql')) |
| 145 | 150 | output = [] |
| 146 | 151 | |
| 147 | 152 | # Post-creation SQL should come before any initial SQL data is loaded. |
diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model/models.py
|
-
|
+
|
|
| | 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 | ordering = ('-pub_date', 'headline') |
| | 13 | |
| | 14 | __test__ = {'API_TESTS': """ |
| | 15 | >>> from django.core import management |
| | 16 | >>> management.call_command('sqlall', 'customsql_model') |
| | 17 | BEGIN; |
| | 18 | CREATE TABLE "customsql_model_article" ( |
| | 19 | "id" integer NOT NULL PRIMARY KEY, |
| | 20 | "headline" varchar(100) NOT NULL, |
| | 21 | "pub_date" datetime NOT NULL |
| | 22 | ) |
| | 23 | ; |
| | 24 | INSERT INTO customsql_model_article (headline, pub_date) VALUES ("Python program becomes self aware", "2006-06-16 11:00:00"); |
| | 25 | COMMIT; |
| | 26 | |
| | 27 | # Syncdb introduces 1 initial data object from article.sql |
| | 28 | >>> Article.objects.all() |
| | 29 | [<Article: Python program becomes self aware>] |
| | 30 | """} |
diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model/sql/article.sql
|
-
|
+
|
|
| | 1 | INSERT INTO customsql_model_article (headline, pub_date) VALUES ("Python program becomes self aware", "2006-06-16 11:00:00"); |
diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model/tests.py
|
-
|
+
|
|
| | 1 | from django.test import TestCase |
| | 2 | from models import Article |
| | 3 | |
| | 4 | class SampleTestCase(TestCase): |
| | 5 | def testClassCustomSQL(self): |
| | 6 | "Check that test case has installed 1 object from SQL" |
| | 7 | self.assertEqual(Article.objects.count(), 1) |
| | 8 | self.assertEquals(str(Article.objects.all()), "[<Article: Python program becomes self aware>]") |
diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model_package/models/__init__.py
|
-
|
+
|
|
| | 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 = 'customsql_model_package' |
| | 13 | ordering = ('-pub_date', 'headline') |
| | 14 | |
| | 15 | __test__ = {'API_TESTS': """ |
| | 16 | >>> from django.core import management |
| | 17 | >>> management.call_command('sqlall', 'customsql_model_package') |
| | 18 | BEGIN; |
| | 19 | CREATE TABLE "customsql_model_package_article" ( |
| | 20 | "id" integer NOT NULL PRIMARY KEY, |
| | 21 | "headline" varchar(100) NOT NULL, |
| | 22 | "pub_date" datetime NOT NULL |
| | 23 | ) |
| | 24 | ; |
| | 25 | INSERT INTO customsql_model_package_article (headline, pub_date) VALUES ("Python program becomes self aware", "2006-06-16 11:00:00"); |
| | 26 | COMMIT; |
| | 27 | |
| | 28 | # Syncdb introduces 1 initial data object from article.sql |
| | 29 | >>> Article.objects.all() |
| | 30 | [<Article: Python program becomes self aware>] |
| | 31 | """} |
diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model_package/sql/article.sql
|
-
|
+
|
|
| | 1 | INSERT INTO customsql_model_package_article (headline, pub_date) VALUES ("Python program becomes self aware", "2006-06-16 11:00:00"); |
diff -r c8000aad4c10 -r 322d8f403154 tests/modeltests/customsql_model_package/tests.py
|
-
|
+
|
|
| | 1 | from django.test import TestCase |
| | 2 | from models import Article |
| | 3 | |
| | 4 | class SampleTestCase(TestCase): |
| | 5 | def testClassCustomSQL(self): |
| | 6 | "Check that test case has installed 1 object from SQL" |
| | 7 | self.assertEqual(Article.objects.count(), 1) |
| | 8 | self.assertEquals(str(Article.objects.all()), "[<Article: Python program becomes self aware>]") |