summaryrefslogtreecommitdiff
path: root/django/db/models
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/models')
-rw-r--r--django/db/models/base.py15
-rw-r--r--django/db/models/fields/__init__.py19
-rw-r--r--django/db/models/fields/related.py23
-rw-r--r--django/db/models/query.py5
-rw-r--r--django/db/models/signals.py10
-rw-r--r--django/db/models/sql/query.py5
6 files changed, 49 insertions, 28 deletions
diff --git a/django/db/models/base.py b/django/db/models/base.py
index 6304e009d3..b3deda13d4 100644
--- a/django/db/models/base.py
+++ b/django/db/models/base.py
@@ -1,6 +1,5 @@
import types
import sys
-import os
from itertools import izip
import django.db.models.manager # Imported to register signal handler.
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS
@@ -16,7 +15,7 @@ from django.db.models.loading import register_models, get_model
from django.utils.translation import ugettext_lazy as _
import django.utils.copycompat as copy
from django.utils.functional import curry, update_wrapper
-from django.utils.encoding import smart_str, force_unicode, smart_unicode
+from django.utils.encoding import smart_str, force_unicode
from django.utils.text import get_text_list, capfirst
from django.conf import settings
@@ -456,7 +455,7 @@ class Model(object):
meta = cls._meta
if origin and not meta.auto_created:
- signals.pre_save.send(sender=origin, instance=self, raw=raw)
+ signals.pre_save.send(sender=origin, instance=self, raw=raw, using=using)
# If we are in a raw save, save the object exactly as presented.
# That means that we don't try to be smart about saving attributes
@@ -540,7 +539,7 @@ class Model(object):
# Signal that the save is complete
if origin and not meta.auto_created:
signals.post_save.send(sender=origin, instance=self,
- created=(not record_exists), raw=raw)
+ created=(not record_exists), raw=raw, using=using)
save_base.alters_data = True
@@ -645,6 +644,8 @@ class Model(object):
return force_unicode(dict(field.flatchoices).get(value, value), strings_only=True)
def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
+ if not self.pk:
+ raise ValueError("get_next/get_previous cannot be used on unsaved objects.")
op = is_next and 'gt' or 'lt'
order = not is_next and '-' or ''
param = smart_str(getattr(self, field.attname))
@@ -744,11 +745,11 @@ class Model(object):
continue
if f.unique:
unique_checks.append((model_class, (name,)))
- if f.unique_for_date:
+ if f.unique_for_date and f.unique_for_date not in exclude:
date_checks.append((model_class, 'date', name, f.unique_for_date))
- if f.unique_for_year:
+ if f.unique_for_year and f.unique_for_year not in exclude:
date_checks.append((model_class, 'year', name, f.unique_for_year))
- if f.unique_for_month:
+ if f.unique_for_month and f.unique_for_month not in exclude:
date_checks.append((model_class, 'month', name, f.unique_for_month))
return unique_checks, date_checks
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 65b60a0173..3c58873035 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -459,6 +459,9 @@ class AutoField(Field):
kwargs['blank'] = True
Field.__init__(self, *args, **kwargs)
+ def get_internal_type(self):
+ return "AutoField"
+
def to_python(self, value):
if value is None:
return value
@@ -795,6 +798,14 @@ class EmailField(CharField):
kwargs['max_length'] = kwargs.get('max_length', 75)
CharField.__init__(self, *args, **kwargs)
+ def formfield(self, **kwargs):
+ # As with CharField, this will cause email validation to be performed twice
+ defaults = {
+ 'form_class': forms.EmailField,
+ }
+ defaults.update(kwargs)
+ return super(EmailField, self).formfield(**defaults)
+
class FilePathField(Field):
description = _("File path")
@@ -1105,6 +1116,14 @@ class URLField(CharField):
CharField.__init__(self, verbose_name, name, **kwargs)
self.validators.append(validators.URLValidator(verify_exists=verify_exists))
+ def formfield(self, **kwargs):
+ # As with CharField, this will cause URL validation to be performed twice
+ defaults = {
+ 'form_class': forms.URLField,
+ }
+ defaults.update(kwargs)
+ return super(URLField, self).formfield(**defaults)
+
class XMLField(TextField):
description = _("XML text")
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 5830a794df..1634d7d974 100644
--- a/django/db/models/fields/related.py
+++ b/django/db/models/fields/related.py
@@ -566,7 +566,7 @@ def create_many_related_manager(superclass, rel=False):
# duplicate data row for symmetrical reverse entries.
signals.m2m_changed.send(sender=rel.through, action='pre_add',
instance=self.instance, reverse=self.reverse,
- model=self.model, pk_set=new_ids)
+ model=self.model, pk_set=new_ids, using=db)
# Add the ones that aren't there already
for obj_id in new_ids:
self.through._default_manager.using(db).create(**{
@@ -578,7 +578,7 @@ def create_many_related_manager(superclass, rel=False):
# duplicate data row for symmetrical reverse entries.
signals.m2m_changed.send(sender=rel.through, action='post_add',
instance=self.instance, reverse=self.reverse,
- model=self.model, pk_set=new_ids)
+ model=self.model, pk_set=new_ids, using=db)
def _remove_items(self, source_field_name, target_field_name, *objs):
# source_col_name: the PK colname in join_table for the source object
@@ -594,14 +594,16 @@ def create_many_related_manager(superclass, rel=False):
old_ids.add(obj.pk)
else:
old_ids.add(obj)
+ # Work out what DB we're operating on
+ db = router.db_for_write(self.through.__class__, instance=self.instance)
+ # Send a signal to the other end if need be.
if self.reverse or source_field_name == self.source_field_name:
# Don't send the signal when we are deleting the
# duplicate data row for symmetrical reverse entries.
signals.m2m_changed.send(sender=rel.through, action="pre_remove",
instance=self.instance, reverse=self.reverse,
- model=self.model, pk_set=old_ids)
+ model=self.model, pk_set=old_ids, using=db)
# Remove the specified objects from the join table
- db = router.db_for_write(self.through.__class__, instance=self.instance)
self.through._default_manager.using(db).filter(**{
source_field_name: self._pk_val,
'%s__in' % target_field_name: old_ids
@@ -611,17 +613,17 @@ def create_many_related_manager(superclass, rel=False):
# duplicate data row for symmetrical reverse entries.
signals.m2m_changed.send(sender=rel.through, action="post_remove",
instance=self.instance, reverse=self.reverse,
- model=self.model, pk_set=old_ids)
+ model=self.model, pk_set=old_ids, using=db)
def _clear_items(self, source_field_name):
+ db = router.db_for_write(self.through.__class__, instance=self.instance)
# source_col_name: the PK colname in join_table for the source object
if self.reverse or source_field_name == self.source_field_name:
# Don't send the signal when we are clearing the
# duplicate data rows for symmetrical reverse entries.
signals.m2m_changed.send(sender=rel.through, action="pre_clear",
instance=self.instance, reverse=self.reverse,
- model=self.model, pk_set=None)
- db = router.db_for_write(self.through.__class__, instance=self.instance)
+ model=self.model, pk_set=None, using=db)
self.through._default_manager.using(db).filter(**{
source_field_name: self._pk_val
}).delete()
@@ -630,7 +632,7 @@ def create_many_related_manager(superclass, rel=False):
# duplicate data rows for symmetrical reverse entries.
signals.m2m_changed.send(sender=rel.through, action="post_clear",
instance=self.instance, reverse=self.reverse,
- model=self.model, pk_set=None)
+ model=self.model, pk_set=None, using=db)
return ManyRelatedManager
@@ -812,6 +814,9 @@ class ForeignKey(RelatedField, Field):
to_field = to_field or (to._meta.pk and to._meta.pk.name)
kwargs['verbose_name'] = kwargs.get('verbose_name', None)
+ if 'db_index' not in kwargs:
+ kwargs['db_index'] = True
+
kwargs['rel'] = rel_class(to, to_field,
related_name=kwargs.pop('related_name', None),
limit_choices_to=kwargs.pop('limit_choices_to', None),
@@ -819,8 +824,6 @@ class ForeignKey(RelatedField, Field):
parent_link=kwargs.pop('parent_link', False))
Field.__init__(self, **kwargs)
- self.db_index = True
-
def validate(self, value, model_instance):
if self.rel.parent_link:
return
diff --git a/django/db/models/query.py b/django/db/models/query.py
index d9fbd9b8cb..9ecfb745fd 100644
--- a/django/db/models/query.py
+++ b/django/db/models/query.py
@@ -1311,7 +1311,8 @@ def delete_objects(seen_objs, using):
# Pre-notify all instances to be deleted.
for pk_val, instance in items:
if not cls._meta.auto_created:
- signals.pre_delete.send(sender=cls, instance=instance)
+ signals.pre_delete.send(sender=cls, instance=instance,
+ using=using)
pk_list = [pk for pk,instance in items]
@@ -1343,7 +1344,7 @@ def delete_objects(seen_objs, using):
setattr(instance, field.attname, None)
if not cls._meta.auto_created:
- signals.post_delete.send(sender=cls, instance=instance)
+ signals.post_delete.send(sender=cls, instance=instance, using=using)
setattr(instance, cls._meta.pk.attname, None)
if forced_managed:
diff --git a/django/db/models/signals.py b/django/db/models/signals.py
index cd0350bc01..48872e7e7f 100644
--- a/django/db/models/signals.py
+++ b/django/db/models/signals.py
@@ -5,12 +5,12 @@ class_prepared = Signal(providing_args=["class"])
pre_init = Signal(providing_args=["instance", "args", "kwargs"])
post_init = Signal(providing_args=["instance"])
-pre_save = Signal(providing_args=["instance", "raw"])
-post_save = Signal(providing_args=["instance", "raw", "created"])
+pre_save = Signal(providing_args=["instance", "raw", "using"])
+post_save = Signal(providing_args=["instance", "raw", "created", "using"])
-pre_delete = Signal(providing_args=["instance"])
-post_delete = Signal(providing_args=["instance"])
+pre_delete = Signal(providing_args=["instance", "using"])
+post_delete = Signal(providing_args=["instance", "using"])
post_syncdb = Signal(providing_args=["class", "app", "created_models", "verbosity", "interactive"])
-m2m_changed = Signal(providing_args=["action", "instance", "reverse", "model", "pk_set"])
+m2m_changed = Signal(providing_args=["action", "instance", "reverse", "model", "pk_set", "using"])
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index 0913399e2a..ec477447f3 100644
--- a/django/db/models/sql/query.py
+++ b/django/db/models/sql/query.py
@@ -1090,10 +1090,7 @@ class Query(object):
# exclude the "foo__in=[]" case from this handling, because
# it's short-circuited in the Where class.
# We also need to handle the case where a subquery is provided
- entry = self.where_class()
- entry.add((Constraint(alias, col, None), 'isnull', True), AND)
- entry.negate()
- self.where.add(entry, AND)
+ self.where.add((Constraint(alias, col, None), 'isnull', False), AND)
if can_reuse is not None:
can_reuse.update(join_list)