Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(unfinished) fixes for Django 2.0 #672

Merged
merged 2 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ matrix:
env: DJANGO="Django>=1.9,<1.10"
- python: "3.3"
env: DJANGO="Django>=1.10,<1.11"
- python: "2.7"
env: DJANGO="https://github.com/django/django/archive/master.tar.gz"
- python: "3.3"
env: DJANGO="Django>=1.11,<1.12"
- python: "3.3"
Expand Down
8 changes: 4 additions & 4 deletions import_export/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
from django.contrib.admin.models import LogEntry, ADDITION, CHANGE, DELETION
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponseRedirect, HttpResponse
try:
from django.urls import reverse
except ImportError: # Django<2.0
from django.core.urlresolvers import reverse
from django.conf import settings
from django.template.defaultfilters import pluralize
from django.utils.decorators import method_decorator
Expand All @@ -37,10 +41,6 @@
except ImportError:
from django.utils.encoding import force_unicode as force_text

try:
from django.core.urlresolvers import reverse
except ImportError:
from django.urls import reverse
SKIP_ADMIN_LOG = getattr(settings, 'IMPORT_EXPORT_SKIP_ADMIN_LOG', False)
TMP_STORAGE_CLASS = getattr(settings, 'IMPORT_EXPORT_TMP_STORAGE_CLASS',
TempFolderStorage)
Expand Down
18 changes: 13 additions & 5 deletions import_export/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ def emit(self, record):
USE_TRANSACTIONS = getattr(settings, 'IMPORT_EXPORT_USE_TRANSACTIONS', True)


def get_related_model(field):
if hasattr(field, 'related_model'):
return field.related_model
# Django 1.6, 1.7
if field.rel:
return field.rel.to


class ResourceOptions(object):
"""
The inner Meta class allows for class-level configuration of how the
Expand Down Expand Up @@ -700,15 +708,15 @@ def __new__(cls, name, bases, attrs):
# the next model.
if isinstance(f, ForeignObjectRel):
if RelatedObject is None:
model = f.related_model
model = get_related_model(f)
else:
# Django < 1.8
model = f.model
else:
if f.rel is None:
if get_related_model(f) is None:
raise KeyError(
'%s is not a relation' % verbose_path)
model = f.rel.to
model = get_related_model(f)

if isinstance(f, ForeignObjectRel):
f = f.field
Expand Down Expand Up @@ -737,10 +745,10 @@ def widget_from_django_field(cls, f, default=widgets.Widget):
internal_type = f.get_internal_type() if callable(getattr(f, "get_internal_type", None)) else ""
if internal_type in ('ManyToManyField', ):
result = functools.partial(widgets.ManyToManyWidget,
model=f.rel.to)
model=get_related_model(f))
if internal_type in ('ForeignKey', 'OneToOneField', ):
result = functools.partial(widgets.ForeignKeyWidget,
model=f.rel.to)
model=get_related_model(f))
if internal_type in ('DecimalField', ):
result = widgets.DecimalWidget
if internal_type in ('DateTimeField', ):
Expand Down
8 changes: 4 additions & 4 deletions tests/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def __str__(self):
@python_2_unicode_compatible
class Book(models.Model):
name = models.CharField('Book name', max_length=100)
author = models.ForeignKey(Author, blank=True, null=True)
author = models.ForeignKey(Author, blank=True, null=True, on_delete=models.CASCADE)
author_email = models.EmailField('Author email', max_length=75, blank=True)
imported = models.BooleanField(default=False)
published = models.DateField('Published', blank=True, null=True)
Expand All @@ -49,20 +49,20 @@ def __str__(self):

@python_2_unicode_compatible
class Child(models.Model):
parent = models.ForeignKey(Parent)
parent = models.ForeignKey(Parent, on_delete=models.CASCADE)
name = models.CharField(max_length=100)

def __str__(self):
return '%s - child of %s' % (self.name, self.parent.name)


class Profile(models.Model):
user = models.OneToOneField('auth.User')
user = models.OneToOneField('auth.User', on_delete=models.CASCADE)
is_private = models.BooleanField(default=True)


class Entry(models.Model):
user = models.ForeignKey('auth.User')
user = models.ForeignKey('auth.User', on_delete=models.CASCADE)


class WithDefault(models.Model):
Expand Down
2 changes: 1 addition & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^admin/', admin.site.urls),
]

urlpatterns += staticfiles_urlpatterns()