Skip to content

Commit

Permalink
Merge pull request #2 from romanosipenko/master
Browse files Browse the repository at this point in the history
Django 1.9+ compatibility
  • Loading branch information
Quard committed May 2, 2018
2 parents 532a4e6 + 3e01315 commit 99a13ae
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion classifier/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = (0, 2, 1)
VERSION = (0, 2, 2)
22 changes: 17 additions & 5 deletions classifier/formsets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import six
from django import VERSION as DJANGO_VERSION
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.forms.models import BaseModelFormSet
Expand Down Expand Up @@ -57,11 +58,22 @@ def get_form_initial(label, i):
required_classifiers = ClassifierModel.objects.filter(
only_one_required=True
)
related_name = (
ClassifierLabelModel
.get_classifier_related_field()
.rel.get_accessor_name()
)

# Django 1.9+
# https://docs.djangoproject.com/en/1.9/releases/1.9/#field-rel-changes
if DJANGO_VERSION[0] == 1 and DJANGO_VERSION[1] < 9:
related_name = (
ClassifierLabelModel
.get_classifier_related_field()
.rel.get_accessor_name()
)
else:
related_name = (
ClassifierLabelModel
.get_classifier_related_field()
.remote_field.get_accessor_name()
)

for i, classifier in enumerate(required_classifiers):
labels = getattr(classifier, related_name)
if not labels.filter(pk__in=exists_items).exists():
Expand Down
2 changes: 1 addition & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Django<=1.11
Django<=2.1
-r maintainer.txt
3 changes: 3 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,13 @@ def get_readme():
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
'Framework :: Django',
'Framework :: Django :: 1.8',
'Framework :: Django :: 1.9',
'Framework :: Django :: 1.10',
'Framework :: Django :: 1.11',
'Framework :: Django :: 2.0',
],
include_package_data=False,
zip_safe=False
Expand Down
16 changes: 12 additions & 4 deletions testapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

@python_2_unicode_compatible
class Contact(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='contacts')
kind = models.ForeignKey('ContactClassifierLabel')
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
related_name='contacts',
on_delete=models.CASCADE
)
kind = models.ForeignKey('ContactClassifierLabel', on_delete=models.CASCADE)
value = models.CharField(max_length=200)

def __str__(self):
Expand All @@ -20,7 +24,11 @@ class ContactClassifier(ClassifierAbstract):


class ContactClassifierLabel(ClassifierLabelAbstract):
classifier = models.ForeignKey(ContactClassifier, related_name='labels')
classifier = models.ForeignKey(
ContactClassifier,
related_name='labels',
on_delete=models.CASCADE
)


# Property - right structure without related_name in label
Expand All @@ -29,7 +37,7 @@ class PropertyClassifier(ClassifierAbstract):


class PropertyClassifierLabel(ClassifierLabelAbstract):
kind = models.ForeignKey(PropertyClassifier)
kind = models.ForeignKey(PropertyClassifier, on_delete=models.CASCADE)


# Magic - wrong structure, no ForeignKey from label to classifier
Expand Down

0 comments on commit 99a13ae

Please sign in to comment.