From c3eabb9f7121e4ae128a801a7404b4676b6cca72 Mon Sep 17 00:00:00 2001 From: Sami Fahed Date: Fri, 29 Dec 2017 15:10:44 +0100 Subject: [PATCH] Add Django 2.0 compatibilty --- reversion_compare/compare.py | 6 +++--- reversion_compare_tests/models.py | 8 ++++---- reversion_compare_tests/settings.py | 2 +- reversion_compare_tests/test_custom_model.py | 5 ++++- reversion_compare_tests/urls.py | 2 +- tox.ini | 3 ++- 6 files changed, 15 insertions(+), 11 deletions(-) diff --git a/reversion_compare/compare.py b/reversion_compare/compare.py index 70c35f6..bced20e 100644 --- a/reversion_compare/compare.py +++ b/reversion_compare/compare.py @@ -110,7 +110,7 @@ def get_object_version(self): return getattr(self.version_record, 'object_version') def get_related(self): - if getattr(self.field, 'rel', None): + if getattr(self.field, 'related_model', None): obj = self.get_object_version().object try: return getattr(obj, self.field.name, None) @@ -165,7 +165,7 @@ def get_many_to_many(self): return {}, {}, [] # TODO: refactor that # Get the related model of the current field: - return self.get_many_to_something(ids, self.field.rel.to) + return self.get_many_to_something(ids, self.field.related_model) def get_many_to_something(self, target_ids, related_model, is_reverse=False): # get instance of reversion.models.Revision(): @@ -260,7 +260,7 @@ def __init__(self, field, field_name, obj, version1, version2, is_reversed): self.obj = obj # is a related field (ForeignKey, ManyToManyField etc.) - self.is_related = getattr(self.field, 'rel', None) is not None + self.is_related = getattr(self.field, 'related_model', None) is not None self.is_reversed = is_reversed if not self.is_related: self.follow = None diff --git a/reversion_compare_tests/models.py b/reversion_compare_tests/models.py index 9ca5ed3..494aae6 100644 --- a/reversion_compare_tests/models.py +++ b/reversion_compare_tests/models.py @@ -62,7 +62,7 @@ def __str__(self): @python_2_unicode_compatible class Car(models.Model): name = models.CharField(max_length=128) - manufacturer = models.ForeignKey(Factory, related_name="cars") + manufacturer = models.ForeignKey(Factory, related_name="cars", on_delete=models.CASCADE) supplier = models.ManyToManyField(Factory, related_name="suppliers", blank=True) def __str__(self): @@ -82,7 +82,7 @@ class Person(models.Model): name = models.CharField(max_length=100) pets = models.ManyToManyField(Pet, blank=True) # If you work someone, its at a build, but maybe not a factory! - workplace = models.ForeignKey(Building, related_name="workers", null=True) + workplace = models.ForeignKey(Building, related_name="workers", null=True, on_delete=models.CASCADE) def __str__(self): return self.name @@ -91,7 +91,7 @@ def __str__(self): @python_2_unicode_compatible class Identity(models.Model): id_numer = models.CharField(max_length=100) - person = models.OneToOneField(Person, related_name='_identity') + person = models.OneToOneField(Person, related_name='_identity', on_delete=models.CASCADE) def __str__(self): return self.id_numer @@ -165,7 +165,7 @@ class TemplateField(models.Model): """ # some field for easy creating revisions text = models.CharField(max_length=20) - template = models.ForeignKey(Person, blank=True, null=True) + template = models.ForeignKey(Person, blank=True, null=True, on_delete=models.CASCADE) """ @python_2_unicode_compatible diff --git a/reversion_compare_tests/settings.py b/reversion_compare_tests/settings.py index 21b8173..945a46f 100644 --- a/reversion_compare_tests/settings.py +++ b/reversion_compare_tests/settings.py @@ -13,7 +13,7 @@ }, },] -MIDDLEWARE_CLASSES = ( +MIDDLEWARE_CLASSES = MIDDLEWARE = ( 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', diff --git a/reversion_compare_tests/test_custom_model.py b/reversion_compare_tests/test_custom_model.py index 5a86881..e9c154d 100644 --- a/reversion_compare_tests/test_custom_model.py +++ b/reversion_compare_tests/test_custom_model.py @@ -30,7 +30,10 @@ ) % err raise ImportError(msg) -from django.core.urlresolvers import reverse +try: + from django.urls import reverse +except: # Django < 1.10 # pragma: no cover + from django.core.urlresolvers import reverse from reversion_compare_tests.models import CustomModel from .utils.test_cases import BaseTestCase diff --git a/reversion_compare_tests/urls.py b/reversion_compare_tests/urls.py index ce504bf..8112ec2 100644 --- a/reversion_compare_tests/urls.py +++ b/reversion_compare_tests/urls.py @@ -15,7 +15,7 @@ url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: - url(r'^admin/', include(admin.site.urls)), + url(r'^admin/', admin.site.urls), url(r'^test_view/(?P\d+)$', SimpleModelHistoryCompareView.as_view() ), diff --git a/tox.ini b/tox.ini index 9b09c7d..21e38fa 100644 --- a/tox.ini +++ b/tox.ini @@ -5,7 +5,7 @@ [tox] envlist = - {py35,py36,pypy3}-django{18,111}{-dmp,} + {py35,py36,pypy3}-django{18,111,2}{-dmp,} [testenv] passenv = TRAVIS TRAVIS_JOB_ID TRAVIS_BRANCH @@ -21,6 +21,7 @@ basepython = deps = django18: Django>=1.8,<1.9 django111: Django>=1.11,<2.0 + django2: Django>=2.0,<3.0 dmp: diff-match-patch -r{toxinidir}/requirements-test.txt