summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2016-03-10 18:21:25 +0100
committerClaude Paroz <claude@2xlibre.net>2016-03-19 09:24:27 +0100
commit983c158da7723eb00a376bd31db76709da4d0260 (patch)
treed5784910f91a4e8a5ee31bd376796c70b6463e37 /django
parent2b3a9414570af623853ca0f819c7d77d0511f22c (diff)
Refs #24227 -- Replaced M2M isinstance checks by field.many_to_many
Thanks Markus Holtermann, Collin Anderson and Tim Graham for the reviews.
Diffstat (limited to 'django')
-rw-r--r--django/contrib/admin/checks.py27
-rw-r--r--django/contrib/admin/options.py8
-rw-r--r--django/contrib/sites/managers.py4
-rw-r--r--django/db/migrations/autodetector.py15
-rw-r--r--django/forms/models.py4
5 files changed, 21 insertions, 37 deletions
diff --git a/django/contrib/admin/checks.py b/django/contrib/admin/checks.py
index 47e40d62ab..3186d8a752 100644
--- a/django/contrib/admin/checks.py
+++ b/django/contrib/admin/checks.py
@@ -104,8 +104,8 @@ class BaseModelAdminChecks(object):
return refer_to_missing_field(field=field_name, option=label,
model=model, obj=obj, id='admin.E002')
else:
- if not isinstance(field, (models.ForeignKey, models.ManyToManyField)):
- return must_be('a ForeignKey or ManyToManyField',
+ if not field.many_to_many and not isinstance(field, models.ForeignKey):
+ return must_be('a foreign key or a many-to-many field',
option=label, obj=obj, id='admin.E003')
else:
return []
@@ -218,11 +218,10 @@ class BaseModelAdminChecks(object):
# be an extra field on the form.
return []
else:
- if (isinstance(field, models.ManyToManyField) and
- not field.remote_field.through._meta.auto_created):
+ if field.many_to_many and not field.remote_field.through._meta.auto_created:
return [
checks.Error(
- "The value of '%s' cannot include the ManyToManyField '%s', "
+ "The value of '%s' cannot include the many-to-many field '%s' "
"because that field manually specifies a relationship model."
% (label, field_name),
obj=obj.__class__,
@@ -295,8 +294,8 @@ class BaseModelAdminChecks(object):
return refer_to_missing_field(field=field_name, option=label,
model=model, obj=obj, id='admin.E019')
else:
- if not isinstance(field, models.ManyToManyField):
- return must_be('a ManyToManyField', option=label, obj=obj, id='admin.E020')
+ if not field.many_to_many:
+ return must_be('a many-to-many field', option=label, obj=obj, id='admin.E020')
else:
return []
@@ -389,23 +388,17 @@ class BaseModelAdminChecks(object):
is a name of existing field and the field is one of the allowed types.
"""
- forbidden_field_types = (
- models.DateTimeField,
- models.ForeignKey,
- models.ManyToManyField
- )
-
try:
field = model._meta.get_field(field_name)
except FieldDoesNotExist:
return refer_to_missing_field(field=field_name, option=label,
model=model, obj=obj, id='admin.E027')
else:
- if isinstance(field, forbidden_field_types):
+ if field.many_to_many or isinstance(field, (models.DateTimeField, models.ForeignKey)):
return [
checks.Error(
"The value of '%s' refers to '%s', which must not be a DateTimeField, "
- "ForeignKey or ManyToManyField." % (label, field_name),
+ "a foreign key, or a many-to-many field." % (label, field_name),
obj=obj.__class__,
id='admin.E028',
)
@@ -629,10 +622,10 @@ class ModelAdminChecks(BaseModelAdminChecks):
id='admin.E108',
)
]
- elif isinstance(field, models.ManyToManyField):
+ elif getattr(field, 'many_to_many', False):
return [
checks.Error(
- "The value of '%s' must not be a ManyToManyField." % label,
+ "The value of '%s' must not be a many-to-many field." % label,
obj=obj.__class__,
id='admin.E109',
)
diff --git a/django/contrib/admin/options.py b/django/contrib/admin/options.py
index 6eb5060414..bdd988f4c7 100644
--- a/django/contrib/admin/options.py
+++ b/django/contrib/admin/options.py
@@ -133,8 +133,8 @@ class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
if db_field.choices:
return self.formfield_for_choice_field(db_field, request, **kwargs)
- # ForeignKey or ManyToManyFields
- if isinstance(db_field, (models.ForeignKey, models.ManyToManyField)):
+ # Foreign key or many-to-many fields
+ if db_field.many_to_many or isinstance(db_field, models.ForeignKey):
# Combine the field kwargs with any options for formfield_overrides.
# Make sure the passed in **kwargs override anything in
# formfield_overrides because **kwargs is more specific, and should
@@ -145,7 +145,7 @@ class BaseModelAdmin(six.with_metaclass(forms.MediaDefiningClass)):
# Get the correct formfield.
if isinstance(db_field, models.ForeignKey):
formfield = self.formfield_for_foreignkey(db_field, request, **kwargs)
- elif isinstance(db_field, models.ManyToManyField):
+ elif db_field.many_to_many:
formfield = self.formfield_for_manytomany(db_field, request, **kwargs)
# For non-raw_id fields, wrap the widget with a wrapper that adds
@@ -1385,7 +1385,7 @@ class ModelAdmin(BaseModelAdmin):
except FieldDoesNotExist:
continue
# We have to special-case M2Ms as a list of comma-separated PKs.
- if isinstance(f, models.ManyToManyField):
+ if f.many_to_many:
initial[k] = initial[k].split(",")
return initial
diff --git a/django/contrib/sites/managers.py b/django/contrib/sites/managers.py
index a4f83777c5..a3a0f07fc3 100644
--- a/django/contrib/sites/managers.py
+++ b/django/contrib/sites/managers.py
@@ -34,10 +34,10 @@ class CurrentSiteManager(models.Manager):
)
]
- if not isinstance(field, (models.ForeignKey, models.ManyToManyField)):
+ if not field.many_to_many and not isinstance(field, (models.ForeignKey)):
return [
checks.Error(
- "CurrentSiteManager cannot use '%s.%s' as it is not a ForeignKey or ManyToManyField." % (
+ "CurrentSiteManager cannot use '%s.%s' as it is not a foreign key or a many-to-many field." % (
self.model._meta.object_name, field_name
),
obj=self,
diff --git a/django/db/migrations/autodetector.py b/django/db/migrations/autodetector.py
index d5b2666575..b7716b208f 100644
--- a/django/db/migrations/autodetector.py
+++ b/django/db/migrations/autodetector.py
@@ -799,8 +799,7 @@ class MigrationAutodetector(object):
# You can't just add NOT NULL fields with no default or fields
# which don't allow empty strings as default.
preserve_default = True
- if (not field.null and not field.has_default() and
- not isinstance(field, models.ManyToManyField) and
+ if (not field.null and not field.has_default() and not field.many_to_many and
not (field.blank and field.empty_strings_allowed)):
field = field.clone()
field.default = self.questioner.ask_not_null_addition(field_name, model_name)
@@ -861,19 +860,13 @@ class MigrationAutodetector(object):
old_field_dec = self.deep_deconstruct(old_field)
new_field_dec = self.deep_deconstruct(new_field)
if old_field_dec != new_field_dec:
- both_m2m = (
- isinstance(old_field, models.ManyToManyField) and
- isinstance(new_field, models.ManyToManyField)
- )
- neither_m2m = (
- not isinstance(old_field, models.ManyToManyField) and
- not isinstance(new_field, models.ManyToManyField)
- )
+ both_m2m = old_field.many_to_many and new_field.many_to_many
+ neither_m2m = not old_field.many_to_many and not new_field.many_to_many
if both_m2m or neither_m2m:
# Either both fields are m2m or neither is
preserve_default = True
if (old_field.null and not new_field.null and not new_field.has_default() and
- not isinstance(new_field, models.ManyToManyField)):
+ not new_field.many_to_many):
field = new_field.clone()
new_default = self.questioner.ask_not_null_alteration(field_name, model_name)
if new_default is not models.NOT_PROVIDED:
diff --git a/django/forms/models.py b/django/forms/models.py
index 4422882315..067f601493 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -79,8 +79,6 @@ def model_to_dict(instance, fields=None, exclude=None):
fields will be excluded from the returned dict, even if they are listed in
the ``fields`` argument.
"""
- # avoid a circular import
- from django.db.models.fields.related import ManyToManyField
opts = instance._meta
data = {}
for f in chain(opts.concrete_fields, opts.virtual_fields, opts.many_to_many):
@@ -90,7 +88,7 @@ def model_to_dict(instance, fields=None, exclude=None):
continue
if exclude and f.name in exclude:
continue
- if isinstance(f, ManyToManyField):
+ if f.many_to_many:
# If the object doesn't have a primary key yet, just use an empty
# list for its m2m fields. Calling f.value_from_object will raise
# an exception.