| 1 |
from django.db import models |
|---|
| 2 |
from django.db import connection |
|---|
| 3 |
|
|---|
| 4 |
class Square(models.Model): |
|---|
| 5 |
root = models.IntegerField() |
|---|
| 6 |
square = models.PositiveIntegerField() |
|---|
| 7 |
|
|---|
| 8 |
def __unicode__(self): |
|---|
| 9 |
return "%s ** 2 == %s" % (self.root, self.square) |
|---|
| 10 |
|
|---|
| 11 |
class Person(models.Model): |
|---|
| 12 |
first_name = models.CharField(max_length=20) |
|---|
| 13 |
last_name = models.CharField(max_length=20) |
|---|
| 14 |
|
|---|
| 15 |
def __unicode__(self): |
|---|
| 16 |
return u'%s %s' % (self.first_name, self.last_name) |
|---|
| 17 |
|
|---|
| 18 |
qn = connection.ops.quote_name |
|---|
| 19 |
|
|---|
| 20 |
__test__ = {'API_TESTS': """ |
|---|
| 21 |
|
|---|
| 22 |
#4896: Test cursor.executemany |
|---|
| 23 |
>>> from django.db import connection |
|---|
| 24 |
>>> cursor = connection.cursor() |
|---|
| 25 |
>>> opts = Square._meta |
|---|
| 26 |
>>> f1, f2 = opts.get_field('root'), opts.get_field('square') |
|---|
| 27 |
>>> query = ('INSERT INTO %s (%s, %s) VALUES (%%s, %%s)' |
|---|
| 28 |
... % (connection.introspection.table_name_converter(opts.db_table), qn(f1.column), qn(f2.column))) |
|---|
| 29 |
>>> cursor.executemany(query, [(i, i**2) for i in range(-5, 6)]) and None or None |
|---|
| 30 |
>>> Square.objects.order_by('root') |
|---|
| 31 |
[<Square: -5 ** 2 == 25>, <Square: -4 ** 2 == 16>, <Square: -3 ** 2 == 9>, <Square: -2 ** 2 == 4>, <Square: -1 ** 2 == 1>, <Square: 0 ** 2 == 0>, <Square: 1 ** 2 == 1>, <Square: 2 ** 2 == 4>, <Square: 3 ** 2 == 9>, <Square: 4 ** 2 == 16>, <Square: 5 ** 2 == 25>] |
|---|
| 32 |
|
|---|
| 33 |
#4765: executemany with params=[] does nothing |
|---|
| 34 |
>>> cursor.executemany(query, []) and None or None |
|---|
| 35 |
>>> Square.objects.count() |
|---|
| 36 |
11 |
|---|
| 37 |
|
|---|
| 38 |
#6254: fetchone, fetchmany, fetchall return strings as unicode objects |
|---|
| 39 |
>>> Person(first_name="John", last_name="Doe").save() |
|---|
| 40 |
>>> Person(first_name="Jane", last_name="Doe").save() |
|---|
| 41 |
>>> Person(first_name="Mary", last_name="Agnelline").save() |
|---|
| 42 |
>>> Person(first_name="Peter", last_name="Parker").save() |
|---|
| 43 |
>>> Person(first_name="Clark", last_name="Kent").save() |
|---|
| 44 |
>>> opts2 = Person._meta |
|---|
| 45 |
>>> f3, f4 = opts2.get_field('first_name'), opts2.get_field('last_name') |
|---|
| 46 |
>>> query2 = ('SELECT %s, %s FROM %s ORDER BY %s' |
|---|
| 47 |
... % (qn(f3.column), qn(f4.column), connection.introspection.table_name_converter(opts2.db_table), |
|---|
| 48 |
... qn(f3.column))) |
|---|
| 49 |
>>> cursor.execute(query2) and None or None |
|---|
| 50 |
>>> cursor.fetchone() |
|---|
| 51 |
(u'Clark', u'Kent') |
|---|
| 52 |
|
|---|
| 53 |
>>> list(cursor.fetchmany(2)) |
|---|
| 54 |
[(u'Jane', u'Doe'), (u'John', u'Doe')] |
|---|
| 55 |
|
|---|
| 56 |
>>> list(cursor.fetchall()) |
|---|
| 57 |
[(u'Mary', u'Agnelline'), (u'Peter', u'Parker')] |
|---|
| 58 |
|
|---|
| 59 |
"""} |
|---|