Commit 68b99c56 authored by Brian May's avatar Brian May

Fix FieldTracker failing with Django 1.10

From upstream pull
requesthttps://github.com/carljm/django-model-utils/issues/232
parent 3896a377
......@@ -2,8 +2,9 @@ from __future__ import unicode_literals
from copy import deepcopy
from django.db import models
import django
from django.core.exceptions import FieldError
from django.db import models
from django.db.models.query_utils import DeferredAttribute
......@@ -62,12 +63,14 @@ class FieldInstanceTracker(object):
)
def init_deferred_fields(self):
self.instance._deferred_fields = []
self.instance._deferred_fields = set()
if hasattr(self.instance, '_deferred') and not self.instance._deferred:
return
class DeferredAttributeTracker(DeferredAttribute):
def __get__(field, instance, owner):
if instance is None:
return field
data = instance.__dict__
if data.get(field.field_name, field) is field:
instance._deferred_fields.remove(field.field_name)
......@@ -76,19 +79,31 @@ class FieldInstanceTracker(object):
self.saved_data[field.field_name] = deepcopy(value)
return data[field.field_name]
for field in self.fields:
field_obj = self.instance.__class__.__dict__.get(field)
if isinstance(field_obj, DeferredAttribute):
self.instance._deferred_fields.append(field)
# Django 1.4
model = None
if hasattr(field_obj, 'model_ref'):
model = field_obj.model_ref()
if django.VERSION >= (1, 8):
self.instance._deferred_fields = self.instance.get_deferred_fields()
for field in self.instance._deferred_fields:
if django.VERSION >= (1, 10):
field_obj = getattr(self.instance.__class__, field)
else:
field_obj = self.instance.__class__.__dict__.get(field)
field_tracker = DeferredAttributeTracker(
field_obj.field_name, model)
field_obj.field_name, None)
setattr(self.instance.__class__, field, field_tracker)
else:
for field in self.fields:
field_obj = self.instance.__class__.__dict__.get(field)
if isinstance(field_obj, DeferredAttribute):
self.instance._deferred_fields.add(field)
# Django 1.4
if django.VERSION >= (1, 5):
model = None
else:
model = field_obj.model_ref()
field_tracker = DeferredAttributeTracker(
field_obj.field_name, model)
setattr(self.instance.__class__, field, field_tracker)
class FieldTracker(object):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment