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

« back to all changes in this revision

Viewing changes to tests/modeltests/field_subclassing/models.py

  • Committer: Bazaar Package Importer
  • Author(s): Chris Lamb
  • Date: 2010-05-21 07:52:55 UTC
  • mfrom: (1.3.6 upstream)
  • mto: This revision was merged to the branch mainline in revision 28.
  • Revision ID: james.westby@ubuntu.com-20100521075255-ii78v1dyfmyu3uzx
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
Tests for field subclassing.
3
3
"""
4
4
 
 
5
from django.core import serializers
5
6
from django.db import models
6
7
from django.utils.encoding import force_unicode
7
 
from django.core import serializers
8
 
from django.core.exceptions import FieldError
9
 
 
10
 
class Small(object):
11
 
    """
12
 
    A simple class to show that non-trivial Python objects can be used as
13
 
    attributes.
14
 
    """
15
 
    def __init__(self, first, second):
16
 
        self.first, self.second = first, second
17
 
 
18
 
    def __unicode__(self):
19
 
        return u'%s%s' % (force_unicode(self.first), force_unicode(self.second))
20
 
 
21
 
    def __str__(self):
22
 
        return unicode(self).encode('utf-8')
23
 
 
24
 
class SmallField(models.Field):
25
 
    """
26
 
    Turns the "Small" class into a Django field. Because of the similarities
27
 
    with normal character fields and the fact that Small.__unicode__ does
28
 
    something sensible, we don't need to implement a lot here.
29
 
    """
30
 
    __metaclass__ = models.SubfieldBase
31
 
 
32
 
    def __init__(self, *args, **kwargs):
33
 
        kwargs['max_length'] = 2
34
 
        super(SmallField, self).__init__(*args, **kwargs)
35
 
 
36
 
    def get_internal_type(self):
37
 
        return 'CharField'
38
 
 
39
 
    def to_python(self, value):
40
 
        if isinstance(value, Small):
41
 
            return value
42
 
        return Small(value[0], value[1])
43
 
 
44
 
    def get_db_prep_save(self, value):
45
 
        return unicode(value)
46
 
 
47
 
    def get_db_prep_lookup(self, lookup_type, value):
48
 
        if lookup_type == 'exact':
49
 
            return force_unicode(value)
50
 
        if lookup_type == 'in':
51
 
            return [force_unicode(v) for v in value]
52
 
        if lookup_type == 'isnull':
53
 
            return []
54
 
        raise FieldError('Invalid lookup type: %r' % lookup_type)
 
8
 
 
9
from fields import Small, SmallField, JSONField
 
10
 
55
11
 
56
12
class MyModel(models.Model):
57
13
    name = models.CharField(max_length=10)
60
16
    def __unicode__(self):
61
17
        return force_unicode(self.name)
62
18
 
 
19
class DataModel(models.Model):
 
20
    data = JSONField()
 
21
 
63
22
__test__ = {'API_TESTS': ur"""
64
23
# Creating a model with custom fields is done as per normal.
65
24
>>> s = Small(1, 2)
92
51
>>> MyModel.objects.filter(data__lt=s)
93
52
Traceback (most recent call last):
94
53
...
95
 
FieldError: Invalid lookup type: 'lt'
 
54
TypeError: Invalid lookup type: 'lt'
96
55
 
97
56
# Serialization works, too.
98
57
>>> stream = serializers.serialize("json", MyModel.objects.all())