Ticket #13328: patch_v3_13328_with_tests.diff

File patch_v3_13328_with_tests.diff, 3.0 KB (added by vbabiy, 15 years ago)

Thrid version of my patch

  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 281963f..0ea9667 100644
    a b class Field(object):  
    132132        memodict[id(self)] = obj
    133133        return obj
    134134
     135    def __getstate__(self):
     136        obj_dict = self.__dict__.copy()
     137        del obj_dict['default']
     138        return obj_dict
     139
     140    def __setstate__(self, data):
     141        self.__dict__.update(data)
     142
     143        # Restore the default
     144        self.default = self._model._meta.get_field_by_name(self.name)[0].default
     145
     146
    135147    def to_python(self, value):
    136148        """
    137149        Converts the input value into the expected Python data type, raising
    class Field(object):  
    233245
    234246    def contribute_to_class(self, cls, name):
    235247        self.set_attributes_from_name(name)
     248        self._model = cls
    236249        cls._meta.add_field(self)
    237250        if self.choices:
    238251            setattr(cls, 'get_%s_display' % self.name, curry(cls._get_FIELD_display, field=self))
  • tests/regressiontests/queryset_pickle/models.py

    diff --git a/tests/regressiontests/queryset_pickle/models.py b/tests/regressiontests/queryset_pickle/models.py
    index ec4bbed..b0cae0a 100644
    a b  
     1import datetime
    12from django.db import models
    23from django.utils.translation import ugettext_lazy as _
    34
     5class Numbers(object):
     6
     7    @classmethod
     8    def get_number(self):
     9        return 2
     10
    411class Group(models.Model):
    512    name = models.CharField(_('name'), max_length=100)
    613
    714class Event(models.Model):
    815    group = models.ForeignKey(Group)
     16
     17class Happening(models.Model):
     18    when = models.DateTimeField(blank=True, default=datetime.datetime.now)
     19    name = models.CharField(blank=True, max_length=100, default=lambda:"test")
     20    number = models.IntegerField(blank=True, default=Numbers.get_number)
  • tests/regressiontests/queryset_pickle/tests.py

    diff --git a/tests/regressiontests/queryset_pickle/tests.py b/tests/regressiontests/queryset_pickle/tests.py
    index 8191403..99ec54f 100644
    a b  
    11import pickle
     2import datetime
    23
    34from django.test import TestCase
    45
    5 from models import Group, Event
    6 
     6from models import Group, Event, Happening
    77
    88class PickleabilityTestCase(TestCase):
    99    def assert_pickles(self, qs):
    class PickleabilityTestCase(TestCase):  
    1212    def test_related_field(self):
    1313        g = Group.objects.create(name="Ponies Who Own Maybachs")
    1414        self.assert_pickles(Event.objects.filter(group=g.id))
     15
     16    def test_datetime_callable_default_all(self):
     17        self.assert_pickles(Happening.objects.all())
     18
     19    def test_datetime_callable_default_filter(self):
     20        self.assert_pickles(Happening.objects.filter(when=datetime.datetime.now()))
     21
     22    def test_lambda_as_default(self):
     23        self.assert_pickles(Happening.objects.filter(name="test"))
     24
     25    def test_callable_as_default(self):
     26        self.assert_pickles(Happening.objects.filter(number=1))
Back to Top