From ba4a6af2775cd2350975ed89a8e7b301aee5be51 Mon Sep 17 00:00:00 2001 From: Dave Hall Date: Tue, 20 Feb 2018 11:01:46 +0000 Subject: [PATCH] Adding support for Django 2.0 BigAutoField. Closes #238. --- watson/models.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/watson/models.py b/watson/models.py index d226726..f18cd8b 100644 --- a/watson/models.py +++ b/watson/models.py @@ -15,16 +15,23 @@ except ImportError: from django.contrib.contenttypes.generic import GenericForeignKey +INTEGER_FIELDS = (models.IntegerField, models.AutoField,) +BIG_INTEGER_FIELDS = (models.BigIntegerField,) + +try: + BIG_INTEGER_FIELDS += (models.BigAutoField,) +except AttributeError: # Django < 2.0. + pass + + def has_int_pk(model): """Tests whether the given model has an integer primary key.""" pk = model._meta.pk return ( - ( - isinstance(pk, (models.IntegerField, models.AutoField)) and - not isinstance(pk, models.BigIntegerField) - ) or ( - isinstance(pk, models.ForeignKey) and has_int_pk(pk.remote_field.model) - ) + isinstance(pk, INTEGER_FIELDS) and + not isinstance(pk, BIG_INTEGER_FIELDS) + ) or ( + isinstance(pk, models.ForeignKey) and has_int_pk(pk.remote_field.model) )