diff options
Diffstat (limited to 'django/db/models/fields')
-rw-r--r-- | django/db/models/fields/__init__.py | 19 | ||||
-rw-r--r-- | django/db/models/fields/related.py | 23 |
2 files changed, 32 insertions, 10 deletions
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index f95efd0ce3..a89b165f6f 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -469,6 +469,9 @@ class AutoField(BaseAutoField): 'invalid': _(u'This value must be an integer.'), } + def get_internal_type(self): + return "AutoField" + def to_python(self, value): if value is None: return value @@ -801,6 +804,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") @@ -1111,6 +1122,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 |