2007-11-05 13:59:42 +00:00
|
|
|
"""
|
|
|
|
Tests for field subclassing.
|
|
|
|
"""
|
|
|
|
|
2010-02-24 19:06:59 +00:00
|
|
|
from django.core import serializers
|
2007-11-05 13:59:42 +00:00
|
|
|
from django.db import models
|
|
|
|
from django.utils.encoding import force_unicode
|
|
|
|
|
2010-02-24 19:06:59 +00:00
|
|
|
from fields import Small, SmallField, JSONField
|
2007-11-05 13:59:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MyModel(models.Model):
|
|
|
|
name = models.CharField(max_length=10)
|
|
|
|
data = SmallField('small field')
|
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
return force_unicode(self.name)
|
|
|
|
|
2010-02-24 19:06:59 +00:00
|
|
|
class DataModel(models.Model):
|
|
|
|
data = JSONField()
|
|
|
|
|
2007-11-05 13:59:42 +00:00
|
|
|
__test__ = {'API_TESTS': ur"""
|
|
|
|
# Creating a model with custom fields is done as per normal.
|
|
|
|
>>> s = Small(1, 2)
|
|
|
|
>>> print s
|
|
|
|
12
|
|
|
|
>>> m = MyModel(name='m', data=s)
|
|
|
|
>>> m.save()
|
|
|
|
|
|
|
|
# Custom fields still have normal field's attributes.
|
|
|
|
>>> m._meta.get_field('data').verbose_name
|
|
|
|
'small field'
|
|
|
|
|
|
|
|
# The m.data attribute has been initialised correctly. It's a Small object.
|
|
|
|
>>> m.data.first, m.data.second
|
|
|
|
(1, 2)
|
|
|
|
|
|
|
|
# The data loads back from the database correctly and 'data' has the right type.
|
|
|
|
>>> m1 = MyModel.objects.get(pk=m.pk)
|
|
|
|
>>> isinstance(m1.data, Small)
|
|
|
|
True
|
|
|
|
>>> print m1.data
|
|
|
|
12
|
|
|
|
|
|
|
|
# We can do normal filtering on the custom field (and will get an error when we
|
|
|
|
# use a lookup type that does not make sense).
|
|
|
|
>>> s1 = Small(1, 3)
|
|
|
|
>>> s2 = Small('a', 'b')
|
|
|
|
>>> MyModel.objects.filter(data__in=[s, s1, s2])
|
|
|
|
[<MyModel: m>]
|
|
|
|
>>> MyModel.objects.filter(data__lt=s)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 02:50:16 +00:00
|
|
|
FieldError: Invalid lookup type: 'lt'
|
2007-11-05 13:59:42 +00:00
|
|
|
|
|
|
|
# Serialization works, too.
|
|
|
|
>>> stream = serializers.serialize("json", MyModel.objects.all())
|
|
|
|
>>> stream
|
|
|
|
'[{"pk": 1, "model": "field_subclassing.mymodel", "fields": {"data": "12", "name": "m"}}]'
|
|
|
|
>>> obj = list(serializers.deserialize("json", stream))[0]
|
|
|
|
>>> obj.object == m
|
|
|
|
True
|
2007-11-29 19:30:49 +00:00
|
|
|
|
|
|
|
# Test retrieving custom field data
|
|
|
|
>>> m.delete()
|
|
|
|
>>> m1 = MyModel(name="1", data=Small(1, 2))
|
|
|
|
>>> m1.save()
|
|
|
|
>>> m2 = MyModel(name="2", data=Small(2, 3))
|
|
|
|
>>> m2.save()
|
|
|
|
>>> for m in MyModel.objects.all(): print unicode(m.data)
|
|
|
|
12
|
|
|
|
23
|
2007-11-05 13:59:42 +00:00
|
|
|
"""}
|