changed name

This commit is contained in:
Wilson Júnior 2011-12-16 11:07:38 -02:00
parent 7c1afd0031
commit 62219d9648
2 changed files with 44 additions and 19 deletions

View File

@ -314,15 +314,17 @@ class QueryFieldList(object):
def __nonzero__(self):
return bool(self.fields)
class SelectResult(object):
class ListResult(object):
"""
Used for .select method in QuerySet
Used for .values_list method in QuerySet
"""
def __init__(self, document_type, cursor, fields, dbfields):
from base import BaseField
from fields import ReferenceField
from fields import ReferenceField, GenericReferenceField
# Caches for optimization
self.ReferenceField = ReferenceField
self.GenericReferenceField = GenericReferenceField
self._cursor = cursor
@ -356,6 +358,10 @@ class SelectResult(object):
if data:
return doc_type._from_son(data)
elif isinstance(field_type, self.GenericReferenceField):
if data and isinstance(data, (dict, pymongo.dbref.DBRef)):
return field_type.dereference(data)
return field_type.to_python(data)
def next(self):
@ -866,9 +872,10 @@ class QuerySet(object):
doc.save()
return doc
def select(self, *fields):
def values_list(self, *fields):
"""
Select a field and make a tuple of element
make a list of elements
.. versionadded:: 0.6
"""
dbfields = self._fields_to_dbfields(fields)
@ -876,7 +883,7 @@ class QuerySet(object):
cursor_args['fields'] = dbfields
cursor = self._build_cursor(**cursor_args)
return SelectResult(self._document, cursor, fields, dbfields)
return ListResult(self._document, cursor, fields, dbfields)
def first(self):
"""Retrieve the first object matching the query.

View File

@ -2910,7 +2910,7 @@ class QueryFieldListTest(unittest.TestCase):
ak = list(Bar.objects(foo__match={'shape': "square", "color": "purple"}))
self.assertEqual([b1], ak)
def test_select(self):
def test_values_list(self):
class TestDoc(Document):
x = IntField()
y = BooleanField()
@ -2921,7 +2921,7 @@ class QueryFieldListTest(unittest.TestCase):
TestDoc(x=20, y=False).save()
TestDoc(x=30, y=True).save()
plist = list(TestDoc.objects.select('x', 'y'))
plist = list(TestDoc.objects.values_list('x', 'y'))
self.assertEqual(len(plist), 3)
self.assertEqual(plist[0], [10, True])
@ -2939,7 +2939,7 @@ class QueryFieldListTest(unittest.TestCase):
UserDoc(name="Eliana", age=37).save()
UserDoc(name="Tayza", age=15).save()
ulist = list(UserDoc.objects.select('name', 'age'))
ulist = list(UserDoc.objects.values_list('name', 'age'))
self.assertEqual(ulist, [
[u'Wilson Jr', 19],
@ -2947,7 +2947,7 @@ class QueryFieldListTest(unittest.TestCase):
[u'Eliana', 37],
[u'Tayza', 15]])
ulist = list(UserDoc.objects.order_by('age').select('name'))
ulist = list(UserDoc.objects.order_by('age').values_list('name'))
self.assertEqual(ulist, [
[u'Tayza'],
@ -2955,7 +2955,7 @@ class QueryFieldListTest(unittest.TestCase):
[u'Eliana'],
[u'Wilson']])
def test_select_embedded(self):
def test_values_list_embedded(self):
class Profile(EmbeddedDocument):
name = StringField()
age = IntField()
@ -2983,19 +2983,19 @@ class QueryFieldListTest(unittest.TestCase):
locale=Locale(city="Brasilia", country="Brazil")).save()
self.assertEqual(
list(Person.objects.order_by('profile.age').select('profile.name')),
list(Person.objects.order_by('profile.age').values_list('profile.name')),
[[u'Wilson Jr'], [u'Gabriel Falcao'],
[u'Lincoln de souza'], [u'Walter cruz']])
ulist = list(Person.objects.order_by('locale.city')
.select('profile.name', 'profile.age', 'locale.city'))
.values_list('profile.name', 'profile.age', 'locale.city'))
self.assertEqual(ulist,
[[u'Lincoln de souza', 28, u'Belo Horizonte'],
[u'Walter cruz', 30, u'Brasilia'],
[u'Wilson Jr', 19, u'Corumba-GO'],
[u'Gabriel Falcao', 23, u'New York']])
def test_select_decimal(self):
def test_values_list_decimal(self):
from decimal import Decimal
class Person(Document):
name = StringField()
@ -3004,11 +3004,11 @@ class QueryFieldListTest(unittest.TestCase):
Person.drop_collection()
Person(name="Wilson Jr", rating=Decimal('1.0')).save()
ulist = list(Person.objects.select('name', 'rating'))
ulist = list(Person.objects.values_list('name', 'rating'))
self.assertEqual(ulist, [[u'Wilson Jr', Decimal('1.0')]])
def test_select_reference_field(self):
def test_values_list_reference_field(self):
class State(Document):
name = StringField()
@ -3024,11 +3024,29 @@ class QueryFieldListTest(unittest.TestCase):
Person(name="Wilson JR", state=s1).save()
plist = list(Person.objects.select('name', 'state'))
plist = list(Person.objects.values_list('name', 'state'))
self.assertEqual(plist, [[u'Wilson JR', s1]])
def test_values_list_generic_reference_field(self):
class State(Document):
name = StringField()
def test_select_db_field(self):
class Person(Document):
name = StringField()
state = GenericReferenceField()
State.drop_collection()
Person.drop_collection()
s1 = State(name="Goias")
s1.save()
Person(name="Wilson JR", state=s1).save()
plist = list(Person.objects.values_list('name', 'state'))
self.assertEqual(plist, [[u'Wilson JR', s1]])
def test_values_list_db_field(self):
class TestDoc(Document):
x = IntField(db_field="y")
y = BooleanField(db_field="x")
@ -3039,7 +3057,7 @@ class QueryFieldListTest(unittest.TestCase):
TestDoc(x=20, y=False).save()
TestDoc(x=30, y=True).save()
plist = list(TestDoc.objects.select('x', 'y'))
plist = list(TestDoc.objects.values_list('x', 'y'))
self.assertEqual(len(plist), 3)
self.assertEqual(plist[0], [10, True])