2006-05-02 01:31:56 +00:00
|
|
|
"""
|
|
|
|
7. The lookup API
|
|
|
|
|
|
|
|
This demonstrates features of the database API.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from django.db import models
|
2007-07-01 04:47:46 +00:00
|
|
|
from django.conf import settings
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
class Article(models.Model):
|
|
|
|
headline = models.CharField(maxlength=100)
|
|
|
|
pub_date = models.DateTimeField()
|
|
|
|
class Meta:
|
|
|
|
ordering = ('-pub_date', 'headline')
|
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-02 01:31:56 +00:00
|
|
|
return self.headline
|
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
__test__ = {'API_TESTS':r"""
|
2006-05-02 01:31:56 +00:00
|
|
|
# Create a couple of Articles.
|
|
|
|
>>> from datetime import datetime
|
|
|
|
>>> a1 = Article(headline='Article 1', pub_date=datetime(2005, 7, 26))
|
|
|
|
>>> a1.save()
|
|
|
|
>>> a2 = Article(headline='Article 2', pub_date=datetime(2005, 7, 27))
|
|
|
|
>>> a2.save()
|
|
|
|
>>> a3 = Article(headline='Article 3', pub_date=datetime(2005, 7, 27))
|
|
|
|
>>> a3.save()
|
|
|
|
>>> a4 = Article(headline='Article 4', pub_date=datetime(2005, 7, 28))
|
|
|
|
>>> a4.save()
|
|
|
|
>>> a5 = Article(headline='Article 5', pub_date=datetime(2005, 8, 1, 9, 0))
|
|
|
|
>>> a5.save()
|
|
|
|
>>> a6 = Article(headline='Article 6', pub_date=datetime(2005, 8, 1, 8, 0))
|
|
|
|
>>> a6.save()
|
|
|
|
>>> a7 = Article(headline='Article 7', pub_date=datetime(2005, 7, 27))
|
|
|
|
>>> a7.save()
|
|
|
|
|
|
|
|
# Each QuerySet gets iterator(), which is a generator that "lazily" returns
|
|
|
|
# results using database-level iteration.
|
|
|
|
>>> for a in Article.objects.iterator():
|
|
|
|
... print a.headline
|
|
|
|
Article 5
|
|
|
|
Article 6
|
|
|
|
Article 4
|
|
|
|
Article 2
|
|
|
|
Article 3
|
|
|
|
Article 7
|
|
|
|
Article 1
|
|
|
|
|
|
|
|
# iterator() can be used on any QuerySet.
|
|
|
|
>>> for a in Article.objects.filter(headline__endswith='4').iterator():
|
|
|
|
... print a.headline
|
|
|
|
Article 4
|
|
|
|
|
|
|
|
# count() returns the number of objects matching search criteria.
|
|
|
|
>>> Article.objects.count()
|
|
|
|
7L
|
|
|
|
>>> Article.objects.filter(pub_date__exact=datetime(2005, 7, 27)).count()
|
|
|
|
3L
|
|
|
|
>>> Article.objects.filter(headline__startswith='Blah blah').count()
|
|
|
|
0L
|
|
|
|
|
2007-02-12 00:16:17 +00:00
|
|
|
# count() should respect sliced query sets.
|
|
|
|
>>> articles = Article.objects.all()
|
|
|
|
>>> articles.count()
|
|
|
|
7L
|
|
|
|
>>> articles[:4].count()
|
|
|
|
4
|
|
|
|
>>> articles[1:100].count()
|
|
|
|
6L
|
|
|
|
>>> articles[10:100].count()
|
|
|
|
0
|
|
|
|
|
2006-06-28 11:37:17 +00:00
|
|
|
# Date and date/time lookups can also be done with strings.
|
|
|
|
>>> Article.objects.filter(pub_date__exact='2005-07-27 00:00:00').count()
|
|
|
|
3L
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
# in_bulk() takes a list of IDs and returns a dictionary mapping IDs
|
|
|
|
# to objects.
|
|
|
|
>>> Article.objects.in_bulk([1, 2])
|
2006-06-04 00:23:51 +00:00
|
|
|
{1: <Article: Article 1>, 2: <Article: Article 2>}
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> Article.objects.in_bulk([3])
|
2006-06-04 00:23:51 +00:00
|
|
|
{3: <Article: Article 3>}
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> Article.objects.in_bulk([1000])
|
|
|
|
{}
|
|
|
|
>>> Article.objects.in_bulk([])
|
|
|
|
{}
|
|
|
|
>>> Article.objects.in_bulk('foo')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AssertionError: in_bulk() must be provided with a list of IDs.
|
|
|
|
>>> Article.objects.in_bulk()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: in_bulk() takes exactly 2 arguments (1 given)
|
|
|
|
>>> Article.objects.in_bulk(headline__startswith='Blah')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
TypeError: in_bulk() got an unexpected keyword argument 'headline__startswith'
|
|
|
|
|
|
|
|
# values() returns a list of dictionaries instead of object instances -- and
|
|
|
|
# you can specify which fields you want to retrieve.
|
|
|
|
>>> Article.objects.values('headline')
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
[{'headline': u'Article 5'}, {'headline': u'Article 6'}, {'headline': u'Article 4'}, {'headline': u'Article 2'}, {'headline': u'Article 3'}, {'headline': u'Article 7'}, {'headline': u'Article 1'}]
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> Article.objects.filter(pub_date__exact=datetime(2005, 7, 27)).values('id')
|
|
|
|
[{'id': 2}, {'id': 3}, {'id': 7}]
|
|
|
|
>>> list(Article.objects.values('id', 'headline')) == [{'id': 5, 'headline': 'Article 5'}, {'id': 6, 'headline': 'Article 6'}, {'id': 4, 'headline': 'Article 4'}, {'id': 2, 'headline': 'Article 2'}, {'id': 3, 'headline': 'Article 3'}, {'id': 7, 'headline': 'Article 7'}, {'id': 1, 'headline': 'Article 1'}]
|
|
|
|
True
|
|
|
|
|
|
|
|
>>> for d in Article.objects.values('id', 'headline'):
|
|
|
|
... i = d.items()
|
|
|
|
... i.sort()
|
|
|
|
... i
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
[('headline', u'Article 5'), ('id', 5)]
|
|
|
|
[('headline', u'Article 6'), ('id', 6)]
|
|
|
|
[('headline', u'Article 4'), ('id', 4)]
|
|
|
|
[('headline', u'Article 2'), ('id', 2)]
|
|
|
|
[('headline', u'Article 3'), ('id', 3)]
|
|
|
|
[('headline', u'Article 7'), ('id', 7)]
|
|
|
|
[('headline', u'Article 1'), ('id', 1)]
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
# You can use values() with iterator() for memory savings, because iterator()
|
|
|
|
# uses database-level iteration.
|
|
|
|
>>> for d in Article.objects.values('id', 'headline').iterator():
|
|
|
|
... i = d.items()
|
|
|
|
... i.sort()
|
|
|
|
... i
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
[('headline', u'Article 5'), ('id', 5)]
|
|
|
|
[('headline', u'Article 6'), ('id', 6)]
|
|
|
|
[('headline', u'Article 4'), ('id', 4)]
|
|
|
|
[('headline', u'Article 2'), ('id', 2)]
|
|
|
|
[('headline', u'Article 3'), ('id', 3)]
|
|
|
|
[('headline', u'Article 7'), ('id', 7)]
|
|
|
|
[('headline', u'Article 1'), ('id', 1)]
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2007-05-31 02:22:41 +00:00
|
|
|
|
|
|
|
# you can use values() even on extra fields
|
|
|
|
>>> for d in Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_one'):
|
|
|
|
... i = d.items()
|
|
|
|
... i.sort()
|
|
|
|
... i
|
|
|
|
[('id', 5), ('id_plus_one', 6)]
|
|
|
|
[('id', 6), ('id_plus_one', 7)]
|
|
|
|
[('id', 4), ('id_plus_one', 5)]
|
|
|
|
[('id', 2), ('id_plus_one', 3)]
|
|
|
|
[('id', 3), ('id_plus_one', 4)]
|
|
|
|
[('id', 7), ('id_plus_one', 8)]
|
|
|
|
[('id', 1), ('id_plus_one', 2)]
|
|
|
|
|
|
|
|
# however, an exception FieldDoesNotExist will still be thrown
|
|
|
|
# if you try to access non-existent field (field that is neither on the model nor extra)
|
|
|
|
>>> Article.objects.extra( select={'id_plus_one' : 'id + 1'} ).values('id', 'id_plus_two')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
FieldDoesNotExist: Article has no field named 'id_plus_two'
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
# if you don't specify which fields, all are returned
|
|
|
|
>>> list(Article.objects.filter(id=5).values()) == [{'id': 5, 'headline': 'Article 5', 'pub_date': datetime(2005, 8, 1, 9, 0)}]
|
|
|
|
True
|
|
|
|
|
|
|
|
# Every DateField and DateTimeField creates get_next_by_FOO() and
|
|
|
|
# get_previous_by_FOO() methods.
|
|
|
|
# In the case of identical date values, these methods will use the ID as a
|
|
|
|
# fallback check. This guarantees that no records are skipped or duplicated.
|
|
|
|
>>> a1.get_next_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 2>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a2.get_next_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 3>
|
2006-06-20 04:16:11 +00:00
|
|
|
>>> a2.get_next_by_pub_date(headline__endswith='6')
|
2006-06-20 02:17:14 +00:00
|
|
|
<Article: Article 6>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a3.get_next_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 7>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a4.get_next_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 6>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a5.get_next_by_pub_date()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2006-05-06 18:36:32 +00:00
|
|
|
DoesNotExist: Article matching query does not exist.
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a6.get_next_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 5>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a7.get_next_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 4>
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
>>> a7.get_previous_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 3>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a6.get_previous_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 4>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a5.get_previous_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 6>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a4.get_previous_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 7>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a3.get_previous_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 2>
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a2.get_previous_by_pub_date()
|
2006-06-04 00:23:51 +00:00
|
|
|
<Article: Article 1>
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
# Underscores and percent signs have special meaning in the underlying
|
2006-08-11 05:20:31 +00:00
|
|
|
# SQL code, but Django handles the quoting of them automatically.
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a8 = Article(headline='Article_ with underscore', pub_date=datetime(2005, 11, 20))
|
|
|
|
>>> a8.save()
|
|
|
|
>>> Article.objects.filter(headline__startswith='Article')
|
2006-06-04 00:23:51 +00:00
|
|
|
[<Article: Article_ with underscore>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 7>, <Article: Article 1>]
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> Article.objects.filter(headline__startswith='Article_')
|
2006-06-04 00:23:51 +00:00
|
|
|
[<Article: Article_ with underscore>]
|
2006-08-11 05:20:31 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> a9 = Article(headline='Article% with percent sign', pub_date=datetime(2005, 11, 21))
|
|
|
|
>>> a9.save()
|
|
|
|
>>> Article.objects.filter(headline__startswith='Article')
|
2006-06-04 00:23:51 +00:00
|
|
|
[<Article: Article% with percent sign>, <Article: Article_ with underscore>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 7>, <Article: Article 1>]
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> Article.objects.filter(headline__startswith='Article%')
|
2006-06-04 00:23:51 +00:00
|
|
|
[<Article: Article% with percent sign>]
|
2006-05-02 01:31:56 +00:00
|
|
|
|
|
|
|
# exclude() is the opposite of filter() when doing lookups:
|
|
|
|
>>> Article.objects.filter(headline__contains='Article').exclude(headline__contains='with')
|
2006-06-04 00:23:51 +00:00
|
|
|
[<Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 7>, <Article: Article 1>]
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> Article.objects.exclude(headline__startswith="Article_")
|
2006-06-04 00:23:51 +00:00
|
|
|
[<Article: Article% with percent sign>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 7>, <Article: Article 1>]
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> Article.objects.exclude(headline="Article 7")
|
2006-06-04 00:23:51 +00:00
|
|
|
[<Article: Article% with percent sign>, <Article: Article_ with underscore>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 1>]
|
2006-08-11 05:20:31 +00:00
|
|
|
|
|
|
|
# Backslashes also have special meaning in the underlying SQL code, but Django
|
|
|
|
# automatically quotes them appropriately.
|
|
|
|
>>> a10 = Article(headline='Article with \\ backslash', pub_date=datetime(2005, 11, 22))
|
|
|
|
>>> a10.save()
|
|
|
|
>>> Article.objects.filter(headline__contains='\\')
|
|
|
|
[<Article: Article with \ backslash>]
|
|
|
|
|
2007-01-23 02:11:08 +00:00
|
|
|
# none() returns an EmptyQuerySet that behaves like any other QuerySet object
|
|
|
|
>>> Article.objects.none()
|
|
|
|
[]
|
|
|
|
>>> Article.objects.none().filter(headline__startswith='Article')
|
|
|
|
[]
|
|
|
|
>>> Article.objects.none().count()
|
|
|
|
0
|
2007-02-10 05:38:38 +00:00
|
|
|
>>> [article for article in Article.objects.none().iterator()]
|
|
|
|
[]
|
2007-01-23 02:11:08 +00:00
|
|
|
|
|
|
|
# using __in with an empty list should return an empty query set
|
|
|
|
>>> Article.objects.filter(id__in=[])
|
|
|
|
[]
|
|
|
|
|
|
|
|
>>> Article.objects.exclude(id__in=[])
|
|
|
|
[<Article: Article with \ backslash>, <Article: Article% with percent sign>, <Article: Article_ with underscore>, <Article: Article 5>, <Article: Article 6>, <Article: Article 4>, <Article: Article 2>, <Article: Article 3>, <Article: Article 7>, <Article: Article 1>]
|
|
|
|
|
2007-02-10 03:39:56 +00:00
|
|
|
# Programming errors are pointed out with nice error messages
|
|
|
|
>>> Article.objects.filter(pub_date_year='2005').count()
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2007-05-06 04:12:08 +00:00
|
|
|
TypeError: Cannot resolve keyword 'pub_date_year' into field. Choices are: id, headline, pub_date
|
2007-02-10 03:39:56 +00:00
|
|
|
|
|
|
|
>>> Article.objects.filter(headline__starts='Article')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
2007-05-06 04:12:08 +00:00
|
|
|
TypeError: Cannot resolve keyword 'headline__starts' into field. Choices are: id, headline, pub_date
|
2007-02-10 03:39:56 +00:00
|
|
|
|
2007-06-27 18:58:10 +00:00
|
|
|
# Create some articles with a bit more interesting headlines for testing field lookups:
|
|
|
|
>>> now = datetime.now()
|
|
|
|
>>> for a in Article.objects.all():
|
|
|
|
... a.delete()
|
|
|
|
>>> a1 = Article(pub_date=now, headline='f')
|
|
|
|
>>> a1.save()
|
|
|
|
>>> a2 = Article(pub_date=now, headline='fo')
|
|
|
|
>>> a2.save()
|
|
|
|
>>> a3 = Article(pub_date=now, headline='foo')
|
|
|
|
>>> a3.save()
|
|
|
|
>>> a4 = Article(pub_date=now, headline='fooo')
|
|
|
|
>>> a4.save()
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> a5 = Article(pub_date=now, headline='hey-Foo')
|
2007-06-27 18:58:10 +00:00
|
|
|
>>> a5.save()
|
|
|
|
|
|
|
|
# zero-or-more
|
|
|
|
>>> Article.objects.filter(headline__regex=r'fo*')
|
|
|
|
[<Article: f>, <Article: fo>, <Article: foo>, <Article: fooo>]
|
|
|
|
>>> Article.objects.filter(headline__iregex=r'fo*')
|
2007-07-01 04:47:46 +00:00
|
|
|
[<Article: f>, <Article: fo>, <Article: foo>, <Article: fooo>, <Article: hey-Foo>]
|
2007-06-27 18:58:10 +00:00
|
|
|
|
|
|
|
# one-or-more
|
|
|
|
>>> Article.objects.filter(headline__regex=r'fo+')
|
|
|
|
[<Article: fo>, <Article: foo>, <Article: fooo>]
|
|
|
|
|
|
|
|
# wildcard
|
|
|
|
>>> Article.objects.filter(headline__regex=r'fooo?')
|
|
|
|
[<Article: foo>, <Article: fooo>]
|
|
|
|
|
|
|
|
# and some more:
|
|
|
|
>>> a6 = Article(pub_date=now, headline='bar')
|
|
|
|
>>> a6.save()
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> a7 = Article(pub_date=now, headline='AbBa')
|
2007-06-27 18:58:10 +00:00
|
|
|
>>> a7.save()
|
|
|
|
>>> a8 = Article(pub_date=now, headline='baz')
|
|
|
|
>>> a8.save()
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> a9 = Article(pub_date=now, headline='baxZ')
|
2007-06-27 18:58:10 +00:00
|
|
|
>>> a9.save()
|
|
|
|
|
|
|
|
# leading anchor
|
|
|
|
>>> Article.objects.filter(headline__regex=r'^b')
|
2007-07-01 04:47:46 +00:00
|
|
|
[<Article: bar>, <Article: baxZ>, <Article: baz>]
|
|
|
|
>>> Article.objects.filter(headline__iregex=r'^a')
|
|
|
|
[<Article: AbBa>]
|
2007-06-27 18:58:10 +00:00
|
|
|
|
|
|
|
# trailing anchor
|
|
|
|
>>> Article.objects.filter(headline__regex=r'z$')
|
|
|
|
[<Article: baz>]
|
|
|
|
>>> Article.objects.filter(headline__iregex=r'z$')
|
2007-07-01 04:47:46 +00:00
|
|
|
[<Article: baxZ>, <Article: baz>]
|
2007-06-27 18:58:10 +00:00
|
|
|
|
|
|
|
# character sets
|
|
|
|
>>> Article.objects.filter(headline__regex=r'ba[rz]')
|
|
|
|
[<Article: bar>, <Article: baz>]
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> Article.objects.filter(headline__regex=r'ba.[RxZ]')
|
|
|
|
[<Article: baxZ>]
|
|
|
|
>>> Article.objects.filter(headline__iregex=r'ba[RxZ]')
|
|
|
|
[<Article: bar>, <Article: baxZ>, <Article: baz>]
|
2007-06-27 18:58:10 +00:00
|
|
|
|
|
|
|
# and yet more:
|
|
|
|
>>> a10 = Article(pub_date=now, headline='foobar')
|
|
|
|
>>> a10.save()
|
|
|
|
>>> a11 = Article(pub_date=now, headline='foobaz')
|
|
|
|
>>> a11.save()
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> a12 = Article(pub_date=now, headline='ooF')
|
2007-06-27 18:58:10 +00:00
|
|
|
>>> a12.save()
|
|
|
|
>>> a13 = Article(pub_date=now, headline='foobarbaz')
|
|
|
|
>>> a13.save()
|
|
|
|
>>> a14 = Article(pub_date=now, headline='zoocarfaz')
|
|
|
|
>>> a14.save()
|
|
|
|
>>> a15 = Article(pub_date=now, headline='barfoobaz')
|
|
|
|
>>> a15.save()
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> a16 = Article(pub_date=now, headline='bazbaRFOO')
|
2007-06-27 18:58:10 +00:00
|
|
|
>>> a16.save()
|
|
|
|
|
|
|
|
# alternation
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> Article.objects.filter(headline__regex=r'oo(f|b)')
|
2007-06-27 18:58:10 +00:00
|
|
|
[<Article: barfoobaz>, <Article: foobar>, <Article: foobarbaz>, <Article: foobaz>]
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> Article.objects.filter(headline__iregex=r'oo(f|b)')
|
|
|
|
[<Article: barfoobaz>, <Article: foobar>, <Article: foobarbaz>, <Article: foobaz>, <Article: ooF>]
|
|
|
|
>>> Article.objects.filter(headline__regex=r'^foo(f|b)')
|
2007-06-27 18:58:10 +00:00
|
|
|
[<Article: foobar>, <Article: foobarbaz>, <Article: foobaz>]
|
|
|
|
|
|
|
|
# greedy matching
|
2007-07-01 04:47:46 +00:00
|
|
|
>>> Article.objects.filter(headline__regex=r'b.*az')
|
|
|
|
[<Article: barfoobaz>, <Article: baz>, <Article: bazbaRFOO>, <Article: foobarbaz>, <Article: foobaz>]
|
|
|
|
>>> Article.objects.filter(headline__iregex=r'b.*ar')
|
|
|
|
[<Article: bar>, <Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobar>, <Article: foobarbaz>]
|
|
|
|
"""}
|
|
|
|
|
2007-06-27 18:58:10 +00:00
|
|
|
|
2007-07-01 04:47:46 +00:00
|
|
|
if settings.DATABASE_ENGINE not in ('mysql', 'mysql_old'):
|
|
|
|
__test__['API_TESTS'] += r"""
|
2007-06-27 18:58:10 +00:00
|
|
|
# grouping and backreferences
|
|
|
|
>>> Article.objects.filter(headline__regex=r'b(.).*b\1')
|
2007-07-01 04:47:46 +00:00
|
|
|
[<Article: barfoobaz>, <Article: bazbaRFOO>, <Article: foobarbaz>]
|
|
|
|
"""
|