Ticket #10262: delete_cascade_option_r9832.diff

File delete_cascade_option_r9832.diff, 3.1 KB (added by Manuel Saelices, 16 years ago)
  • django/db/models/base.py

     
    1111import django.db.models.manager     # Imported to register signal handler.
    1212from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError
    1313from django.db.models.fields import AutoField, FieldDoesNotExist
    14 from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField
     14from django.db.models.fields.related import OneToOneRel, ManyToOneRel, OneToOneField, RelatedObject
    1515from django.db.models.query import delete_objects, Q, CollectedObjects
    1616from django.db.models.options import Options
    1717from django.db import connection, transaction, DatabaseError
     
    456456    def delete(self):
    457457        assert self._get_pk_val() is not None, "%s object can't be deleted because its %s attribute is set to None." % (self._meta.object_name, self._meta.pk.attname)
    458458
     459        # Clear all related object with delete_cascade=False
     460        for field in self._meta.get_all_fields():
     461            if isinstance(field, RelatedObject) and not field.field.delete_cascade:
     462                related_manager = getattr(self, field.get_accessor_name())
     463                related_manager.clear()
     464
    459465        # Find all the objects than need to be deleted.
    460466        seen_objs = CollectedObjects()
    461467        self._collect_sub_objects(seen_objs)
  • django/db/models/options.py

     
    283283            raise FieldDoesNotExist('%s has no field named %r'
    284284                    % (self.object_name, name))
    285285
     286    def get_all_fields(self):
     287        """
     288        Returns a list of all fields that are possible for this model
     289        (including reverse relation names).
     290        """
     291        try:
     292            cache = self._name_map
     293        except AttributeError:
     294            cache = self.init_name_map()
     295        fields = [ r[0] for r in cache.values() ]
     296        return fields
     297
    286298    def get_all_field_names(self):
    287299        """
    288300        Returns a list of all field names that are possible for this model
  • django/db/models/fields/related.py

     
    646646            limit_choices_to=kwargs.pop('limit_choices_to', None),
    647647            lookup_overrides=kwargs.pop('lookup_overrides', None),
    648648            parent_link=kwargs.pop('parent_link', False))
     649
     650        self.delete_cascade = kwargs.pop('delete_cascade', True)
     651
    649652        Field.__init__(self, **kwargs)
    650653
    651654        self.db_index = True
     655        assert self.null or self.delete_cascade, "%s cannot define a not null relation (null=False) without deleting cascade (delete_cascade=False)" % self.__class__.__name__
    652656
    653657    def get_attname(self):
    654658        return '%s_id' % self.name
Back to Top