~ubuntu-branches/ubuntu/quantal/python-django/quantal

« back to all changes in this revision

Viewing changes to tests/regressiontests/reverse_single_related/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant, Eddy Mulyono
  • Date: 2008-09-16 12:18:47 UTC
  • mfrom: (1.1.5 upstream) (4.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080916121847-mg225rg5mnsdqzr0
Tags: 1.0-1ubuntu1
* Merge from Debian (LP: #264191), remaining changes:
  - Run test suite on build.

[Eddy Mulyono]
* Update patch to workaround network test case failures.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""
 
2
Regression tests for an object that cannot access a single related object due
 
3
to a restrictive default manager.
 
4
"""
 
5
 
 
6
from django.db import models
 
7
 
 
8
 
 
9
class SourceManager(models.Manager):
 
10
    def get_query_set(self):
 
11
        return super(SourceManager, self).get_query_set().filter(is_public=True)
 
12
 
 
13
class Source(models.Model):
 
14
    is_public = models.BooleanField()
 
15
    objects = SourceManager()
 
16
 
 
17
class Item(models.Model):
 
18
    source = models.ForeignKey(Source)
 
19
 
 
20
 
 
21
__test__ = {'API_TESTS':"""
 
22
 
 
23
>>> public_source = Source.objects.create(is_public=True)
 
24
>>> public_item = Item.objects.create(source=public_source)
 
25
 
 
26
>>> private_source = Source.objects.create(is_public=False)
 
27
>>> private_item = Item.objects.create(source=private_source)
 
28
 
 
29
# Only one source is available via all() due to the custom default manager.
 
30
 
 
31
>>> Source.objects.all()
 
32
[<Source: Source object>]
 
33
 
 
34
>>> public_item.source
 
35
<Source: Source object>
 
36
 
 
37
# Make sure that an item can still access its related source even if the default
 
38
# manager doesn't normally allow it.
 
39
 
 
40
>>> private_item.source
 
41
<Source: Source object>
 
42
 
 
43
# If the manager is marked "use_for_related_fields", it'll get used instead
 
44
# of the "bare" queryset. Usually you'd define this as a property on the class,
 
45
# but this approximates that in a way that's easier in tests.
 
46
 
 
47
>>> Source.objects.use_for_related_fields = True
 
48
>>> private_item = Item.objects.get(pk=private_item.pk)
 
49
>>> private_item.source
 
50
Traceback (most recent call last):
 
51
    ...
 
52
DoesNotExist: Source matching query does not exist.
 
53
 
 
54
"""}