diff --git a/AUTHORS b/AUTHORS index b06bbd69771..34260573b29 100644 --- a/AUTHORS +++ b/AUTHORS @@ -57,6 +57,7 @@ answer newbie questions, and generally made Django that much better: David Avsajanishvili Mike Axiak Niran Babalola + Vitaly Babiy Morten Bagai Jeff Balogh Mikaƫl Barbero diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 281963f3f5c..1dc8dfe8586 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -132,6 +132,19 @@ class Field(object): memodict[id(self)] = obj return obj + def __getstate__(self): + "Don't try to pickle a callable default value" + obj_dict = self.__dict__.copy() + del obj_dict['default'] + return obj_dict + + def __setstate__(self, data): + "When unpickling, restore the callable default" + self.__dict__.update(data) + + # Restore the default + self.default = self.model._meta.get_field_by_name(self.name)[0].default + def to_python(self, value): """ Converts the input value into the expected Python data type, raising @@ -233,6 +246,7 @@ class Field(object): def contribute_to_class(self, cls, name): self.set_attributes_from_name(name) + self.model = cls cls._meta.add_field(self) if self.choices: setattr(cls, 'get_%s_display' % self.name, curry(cls._get_FIELD_display, field=self)) diff --git a/django/db/models/fields/proxy.py b/django/db/models/fields/proxy.py index 31a31e3c3cb..10da0dd4318 100644 --- a/django/db/models/fields/proxy.py +++ b/django/db/models/fields/proxy.py @@ -10,7 +10,10 @@ class OrderWrt(fields.IntegerField): A proxy for the _order database field that is used when Meta.order_with_respect_to is specified. """ - name = '_order' - attname = '_order' - column = '_order' + def __init__(self, model, *args, **kwargs): + super(OrderWrt, self).__init__(*args, **kwargs) + self.model = model + self.attname = '_order' + self.column = '_order' + self.name = '_order' diff --git a/django/db/models/options.py b/django/db/models/options.py index 01a13b4ac30..f9cfeb0f04e 100644 --- a/django/db/models/options.py +++ b/django/db/models/options.py @@ -104,11 +104,11 @@ class Options(object): self.db_table = "%s_%s" % (self.app_label, self.module_name) self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) - def _prepare(self, model): if self.order_with_respect_to: self.order_with_respect_to = self.get_field(self.order_with_respect_to) self.ordering = ('_order',) + self._order = OrderWrt(model) else: self.order_with_respect_to = None @@ -331,7 +331,7 @@ class Options(object): for f, model in self.get_fields_with_model(): cache[f.name] = (f, model, True, False) if self.order_with_respect_to: - cache['_order'] = OrderWrt(), None, True, False + cache['_order'] = self._order, None, True, False if app_cache_ready(): self._name_map = cache return cache diff --git a/tests/regressiontests/queryset_pickle/models.py b/tests/regressiontests/queryset_pickle/models.py index ec4bbed0967..b0cae0a41f2 100644 --- a/tests/regressiontests/queryset_pickle/models.py +++ b/tests/regressiontests/queryset_pickle/models.py @@ -1,8 +1,20 @@ +import datetime from django.db import models from django.utils.translation import ugettext_lazy as _ +class Numbers(object): + + @classmethod + def get_number(self): + return 2 + class Group(models.Model): name = models.CharField(_('name'), max_length=100) class Event(models.Model): group = models.ForeignKey(Group) + +class Happening(models.Model): + when = models.DateTimeField(blank=True, default=datetime.datetime.now) + name = models.CharField(blank=True, max_length=100, default=lambda:"test") + number = models.IntegerField(blank=True, default=Numbers.get_number) diff --git a/tests/regressiontests/queryset_pickle/tests.py b/tests/regressiontests/queryset_pickle/tests.py index 8191403be78..8384df56475 100644 --- a/tests/regressiontests/queryset_pickle/tests.py +++ b/tests/regressiontests/queryset_pickle/tests.py @@ -1,8 +1,9 @@ import pickle +import datetime from django.test import TestCase -from models import Group, Event +from models import Group, Event, Happening class PickleabilityTestCase(TestCase): @@ -12,3 +13,15 @@ class PickleabilityTestCase(TestCase): def test_related_field(self): g = Group.objects.create(name="Ponies Who Own Maybachs") self.assert_pickles(Event.objects.filter(group=g.id)) + + def test_datetime_callable_default_all(self): + self.assert_pickles(Happening.objects.all()) + + def test_datetime_callable_default_filter(self): + self.assert_pickles(Happening.objects.filter(when=datetime.datetime.now())) + + def test_lambda_as_default(self): + self.assert_pickles(Happening.objects.filter(name="test")) + + def test_callable_as_default(self): + self.assert_pickles(Happening.objects.filter(number=1))