Commit 3fb57d47 authored by Russell Keith-Magee's avatar Russell Keith-Magee
Browse files

Fixed #13328 -- Ensured that querysets on models with callable defaults can be...

Fixed #13328 -- Ensured that querysets on models with callable defaults can be pickled. No, really this time. Thanks to Alex for his help brainstorming the solution.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13013 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent e8eac329
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -73,9 +73,6 @@ class BaseDatabaseCreation(object):
                else:
                    field_output.extend(ref_output)
            table_output.append(' '.join(field_output))
        if opts.order_with_respect_to:
            table_output.append(style.SQL_FIELD(qn('_order')) + ' ' + \
                style.SQL_COLTYPE(models.IntegerField().db_type(connection=self.connection)))
        for field_constraints in opts.unique_together:
            table_output.append(style.SQL_KEYWORD('UNIQUE') + ' (%s)' % \
                ", ".join([style.SQL_FIELD(qn(opts.get_field(f).column)) for f in field_constraints]))
+7 −3
Original line number Diff line number Diff line
@@ -504,6 +504,13 @@ class Model(object):
                else:
                    record_exists = False
            if not pk_set or not record_exists:
                if meta.order_with_respect_to:
                    # If this is a model with an order_with_respect_to
                    # autopopulate the _order field
                    field = meta.order_with_respect_to
                    order_value = manager.using(using).filter(**{field.name: getattr(self, field.attname)}).count()
                    setattr(self, '_order', order_value)

                if not pk_set:
                    if force_update:
                        raise ValueError("Cannot force an update in save() with no primary key.")
@@ -513,9 +520,6 @@ class Model(object):
                    values = [(f, f.get_db_prep_save(raw and getattr(self, f.attname) or f.pre_save(self, True), connection=connection))
                        for f in meta.local_fields]

                if meta.order_with_respect_to:
                    field = meta.order_with_respect_to
                    values.append((meta.get_field_by_name('_order')[0], manager.using(using).filter(**{field.name: getattr(self, field.attname)}).count()))
                record_exists = False

                update_pk = bool(meta.has_auto_field and not pk_set)
+0 −16
Original line number Diff line number Diff line
@@ -132,22 +132,6 @@ 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
        try:
            self.default = self.model._meta.get_field(self.name).default
        except FieldDoesNotExist:
            self.default = NOT_PROVIDED

    def to_python(self, value):
        """
        Converts the input value into the expected Python data type, raising
+3 −5
Original line number Diff line number Diff line
@@ -11,9 +11,7 @@ class OrderWrt(fields.IntegerField):
    Meta.order_with_respect_to is specified.
    """

    def __init__(self, model, *args, **kwargs):
    def __init__(self, *args, **kwargs):
        kwargs['name'] = '_order'
        kwargs['editable'] = False
        super(OrderWrt, self).__init__(*args, **kwargs)
        self.model = model
        self.attname = '_order'
        self.column = '_order'
        self.name = '_order'
+1 −3
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ class Options(object):
        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)
            model.add_to_class('_order', OrderWrt())
        else:
            self.order_with_respect_to = None

@@ -330,8 +330,6 @@ class Options(object):
            cache[f.name] = (f, model, True, True)
        for f, model in self.get_fields_with_model():
            cache[f.name] = (f, model, True, False)
        if self.order_with_respect_to:
            cache['_order'] = self._order, None, True, False
        if app_cache_ready():
            self._name_map = cache
        return cache
Loading