mirror of
https://github.com/django/django.git
synced 2025-07-04 09:49:12 +00:00
[soc2010/query-refactor] Added a ListField, currently only works on MongoDB.
git-svn-id: http://code.djangoproject.com/svn/django/branches/soc2010/query-refactor@13441 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
9c6e1c89c5
commit
9944d8d5ef
@ -1,7 +1,8 @@
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
|
from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
from django.db.models.loading import get_apps, get_app, get_models, get_model, register_models
|
from django.db.models.loading import (get_apps, get_app, get_models, get_model,
|
||||||
|
register_models)
|
||||||
from django.db.models.query import Q
|
from django.db.models.query import Q
|
||||||
from django.db.models.expressions import F
|
from django.db.models.expressions import F
|
||||||
from django.db.models.manager import Manager
|
from django.db.models.manager import Manager
|
||||||
@ -10,7 +11,9 @@ from django.db.models.aggregates import *
|
|||||||
from django.db.models.fields import *
|
from django.db.models.fields import *
|
||||||
from django.db.models.fields.subclassing import SubfieldBase
|
from django.db.models.fields.subclassing import SubfieldBase
|
||||||
from django.db.models.fields.files import FileField, ImageField
|
from django.db.models.fields.files import FileField, ImageField
|
||||||
from django.db.models.fields.related import ForeignKey, OneToOneField, ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel
|
from django.db.models.fields.related import (ForeignKey, OneToOneField,
|
||||||
|
ManyToManyField, ManyToOneRel, ManyToManyRel, OneToOneRel)
|
||||||
|
from django.db.models.fields.structures import ListField
|
||||||
from django.db.models import signals
|
from django.db.models import signals
|
||||||
|
|
||||||
# Admin stages.
|
# Admin stages.
|
||||||
|
@ -19,3 +19,15 @@ class Group(models.Model):
|
|||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
year_formed = models.IntegerField(null=True)
|
year_formed = models.IntegerField(null=True)
|
||||||
|
|
||||||
|
|
||||||
|
class Post(models.Model):
|
||||||
|
id = models.NativeAutoField(primary_key=True)
|
||||||
|
title = models.CharField(max_length=255)
|
||||||
|
|
||||||
|
tags = models.ListField(
|
||||||
|
models.CharField(max_length=255)
|
||||||
|
)
|
||||||
|
|
||||||
|
magic_numbers = models.ListField(
|
||||||
|
models.IntegerField()
|
||||||
|
)
|
||||||
|
@ -2,10 +2,18 @@ from django.db import connection, UnsupportedDatabaseOperation
|
|||||||
from django.db.models import Count, Sum, F, Q
|
from django.db.models import Count, Sum, F, Q
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
|
||||||
from models import Artist, Group
|
from models import Artist, Group, Post
|
||||||
|
|
||||||
|
|
||||||
class MongoTestCase(TestCase):
|
class MongoTestCase(TestCase):
|
||||||
|
def assert_unsupported(self, obj):
|
||||||
|
if callable(obj):
|
||||||
|
# Queryset wrapped in a function (for aggregates and such)
|
||||||
|
self.assertRaises(UnsupportedDatabaseOperation, obj)
|
||||||
|
else:
|
||||||
|
# Just a queryset that blows up on evaluation
|
||||||
|
self.assertRaises(UnsupportedDatabaseOperation, list, obj)
|
||||||
|
|
||||||
def test_create(self):
|
def test_create(self):
|
||||||
b = Artist.objects.create(name="Bruce Springsteen", good=True)
|
b = Artist.objects.create(name="Bruce Springsteen", good=True)
|
||||||
self.assertTrue(b.pk is not None)
|
self.assertTrue(b.pk is not None)
|
||||||
@ -359,15 +367,7 @@ class MongoTestCase(TestCase):
|
|||||||
# Ensure that closing a connection that was never established doesn't
|
# Ensure that closing a connection that was never established doesn't
|
||||||
# blow up.
|
# blow up.
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
def assert_unsupported(self, obj):
|
|
||||||
if callable(obj):
|
|
||||||
# Queryset wrapped in a function (for aggregates and such)
|
|
||||||
self.assertRaises(UnsupportedDatabaseOperation, obj)
|
|
||||||
else:
|
|
||||||
# Just a queryset that blows up on evaluation
|
|
||||||
self.assertRaises(UnsupportedDatabaseOperation, list, obj)
|
|
||||||
|
|
||||||
def test_unsupported_ops(self):
|
def test_unsupported_ops(self):
|
||||||
self.assert_unsupported(
|
self.assert_unsupported(
|
||||||
Artist.objects.filter(current_group__name="The Beatles")
|
Artist.objects.filter(current_group__name="The Beatles")
|
||||||
@ -396,3 +396,55 @@ class MongoTestCase(TestCase):
|
|||||||
self.assert_unsupported(
|
self.assert_unsupported(
|
||||||
Artist.objects.filter(Q(pk=0) | Q(pk=1))
|
Artist.objects.filter(Q(pk=0) | Q(pk=1))
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_list_field(self):
|
||||||
|
p = Post.objects.create(
|
||||||
|
title="Django ORM grows MongoDB support",
|
||||||
|
tags=["python", "django", "mongodb", "web"]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertEqual(p.tags, ["python", "django", "mongodb", "web"])
|
||||||
|
|
||||||
|
p = Post.objects.get(pk=p.pk)
|
||||||
|
self.assertEqual(p.tags, ["python", "django", "mongodb", "web"])
|
||||||
|
|
||||||
|
p = Post.objects.create(
|
||||||
|
title="Rails 3.0 Released",
|
||||||
|
tags=["ruby", "rails", "release", "web"],
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Post.objects.filter(tags="web"), [
|
||||||
|
"Django ORM grows MongoDB support",
|
||||||
|
"Rails 3.0 Released",
|
||||||
|
],
|
||||||
|
lambda p: p.title,
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Post.objects.filter(tags="python"), [
|
||||||
|
"Django ORM grows MongoDB support",
|
||||||
|
],
|
||||||
|
lambda p: p.title
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertRaises(ValueError,
|
||||||
|
lambda: Post.objects.create(magic_numbers=["a"])
|
||||||
|
)
|
||||||
|
|
||||||
|
p = Post.objects.create(
|
||||||
|
title="Simon the Wizard",
|
||||||
|
magic_numbers=["42"]
|
||||||
|
)
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Post.objects.filter(magic_numbers=42), [
|
||||||
|
"Simon the Wizard",
|
||||||
|
],
|
||||||
|
lambda p: p.title,
|
||||||
|
)
|
||||||
|
self.assertQuerysetEqual(
|
||||||
|
Post.objects.filter(magic_numbers="42"), [
|
||||||
|
"Simon the Wizard",
|
||||||
|
],
|
||||||
|
lambda p: p.title,
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user