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

Fix django 2.0 compatibility #47

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ MANIFEST
*egg*
*.bak
*.tmproj
.idea
26 changes: 21 additions & 5 deletions genericadmin/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
except ImportError:
from django.contrib.admin.options import IS_POPUP_VAR
from django.core.exceptions import ObjectDoesNotExist
from django.views.generic import RedirectView

JS_PATH = getattr(settings, 'GENERICADMIN_JS', 'genericadmin/js/')

Expand Down Expand Up @@ -63,7 +64,12 @@ def get_generic_field_list(self, request, prefix=''):
fields['prefix'] = prefix
field_list.append(fields)
else:
for field in self.model._meta.virtual_fields:
# virtual_fields were deprecated in django 2.0
try:
fields = self.model._meta.virtual_fields
except AttributeError:
fields = self.model._meta.private_fields
for field in fields:
if isinstance(field, GenericForeignKey) and \
field.ct_field not in exclude and field.fk_field not in exclude:
field_list.append({
Expand All @@ -74,10 +80,16 @@ def get_generic_field_list(self, request, prefix=''):
})

if hasattr(self, 'inlines') and len(self.inlines) > 0:
for FormSet, inline in zip(self.get_formsets_with_inlines(request), self.get_inline_instances(request)):
if hasattr(inline, 'get_generic_field_list'):
prefix = FormSet.get_default_prefix()
field_list = field_list + inline.get_generic_field_list(request, prefix)
try:
for FormSet, inline in zip(self.get_formsets_with_inlines(request), self.get_inline_instances(request)):
if hasattr(inline, 'get_generic_field_list'):
prefix = FormSet.get_default_prefix()
field_list = field_list + inline.get_generic_field_list(request, prefix)
except AttributeError:
for FormSet, inline in self.get_formsets_with_inlines(request):
if hasattr(inline, 'get_generic_field_list'):
prefix = FormSet.get_default_prefix()
field_list = field_list + inline.get_generic_field_list(request, prefix)

return field_list

Expand All @@ -90,6 +102,10 @@ def wrapper(*args, **kwargs):
custom_urls = [
url(r'^obj-data/$', wrap(self.generic_lookup), name='admin_genericadmin_obj_lookup'),
url(r'^genericadmin-init/$', wrap(self.genericadmin_js_init), name='admin_genericadmin_init'),

# to work with django 2.0
url(r'^../obj-data/$', wrap(self.generic_lookup), name='admin_genericadmin_obj_lookup_change'),
url(r'^../genericadmin-init/change/$', wrap(self.genericadmin_js_init), name='admin_genericadmin_init'),
]
return custom_urls + super(BaseGenericModelAdmin, self).get_urls()

Expand Down
8 changes: 7 additions & 1 deletion genericadmin/static/genericadmin/js/genericadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@
},

getLookupUrl: function(cID) {
return '../../../' + this.url_array[cID][0] + '/' + this.getLookupUrlParams(cID);
var forword = '../../../';
var suffix = '/change/';
var href = window.location.href;
if (href.indexOf(suffix, href.length - suffix.length) !== -1) {
forword += '../';
}
return forword + this.url_array[cID][0] + '/' + this.getLookupUrlParams(cID);
},

getFkId: function() {
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def convert_readme():

setup(
name='django-genericadmin',
version='0.7.0',
version='0.7.1',
description="Adds support for generic relations within Django's admin interface.",
author='Weston Nielson, Jan Schrewe, Arthur Hanson',
author_email='[email protected], [email protected], [email protected]',
Expand Down