971 | | for sub_obj in getattr(self, 'get_%s_list' % rel_opts_name)(): |
972 | | sub_obj.delete() |
973 | | for related in opts.get_all_related_many_to_many_objects(): |
| 972 | if getattr(self, 'get_%s_count' % related.get_method_name_part())(): |
| 973 | has_nonfollows= True |
| 974 | break |
| 975 | |
| 976 | if has_nonfollows and self._meta.restrict_delete: |
| 977 | raise exceptions.IntegrityError, "The requested object could not be deleted because it is referenced by other objects." |
| 978 | else: |
| 979 | # Run any pre-delete hooks. |
| 980 | if hasattr(self, '_pre_delete'): |
| 981 | self._pre_delete() |
| 982 | cursor = db.db.cursor() |
| 983 | for related in opts.get_all_related_objects(): |
| 984 | rel_opts_name = related.get_method_name_part() |
| 985 | if isinstance(related.field.rel, OneToOne): |
| 986 | try: |
| 987 | sub_obj = getattr(self, 'get_%s' % rel_opts_name)() |
| 988 | except ObjectDoesNotExist: |
| 989 | pass |
| 990 | else: |
| 991 | sub_obj.delete() |
| 992 | else: |
| 993 | for sub_obj in getattr(self, 'get_%s_list' % rel_opts_name)(): |
| 994 | sub_obj.delete() |
| 995 | for related in opts.get_all_related_many_to_many_objects(): |
| 996 | cursor.execute("DELETE FROM %s WHERE %s=%%s" % \ |
| 997 | (db.db.quote_name(related.field.get_m2m_db_table(related.opts)), |
| 998 | db.db.quote_name(self._meta.object_name.lower() + '_id')), [getattr(self, opts.pk.attname)]) |
| 999 | for f in opts.many_to_many: |
| 1000 | cursor.execute("DELETE FROM %s WHERE %s=%%s" % \ |
| 1001 | (db.db.quote_name(f.get_m2m_db_table(opts)), |
| 1002 | db.db.quote_name(self._meta.object_name.lower() + '_id')), |
| 1003 | [getattr(self, opts.pk.attname)]) |
975 | | (db.db.quote_name(related.field.get_m2m_db_table(related.opts)), |
976 | | db.db.quote_name(self._meta.object_name.lower() + '_id')), [getattr(self, opts.pk.attname)]) |
977 | | for f in opts.many_to_many: |
978 | | cursor.execute("DELETE FROM %s WHERE %s=%%s" % \ |
979 | | (db.db.quote_name(f.get_m2m_db_table(opts)), |
980 | | db.db.quote_name(self._meta.object_name.lower() + '_id')), |
| 1005 | (db.db.quote_name(opts.db_table), db.db.quote_name(opts.pk.column)), |
982 | | cursor.execute("DELETE FROM %s WHERE %s=%%s" % \ |
983 | | (db.db.quote_name(opts.db_table), db.db.quote_name(opts.pk.column)), |
984 | | [getattr(self, opts.pk.attname)]) |
985 | | db.db.commit() |
986 | | setattr(self, opts.pk.attname, None) |
987 | | for f in opts.fields: |
988 | | if isinstance(f, FileField) and getattr(self, f.attname): |
989 | | file_name = getattr(self, 'get_%s_filename' % f.name)() |
990 | | # If the file exists and no other object of this type references it, |
991 | | # delete it from the filesystem. |
992 | | if os.path.exists(file_name) and not opts.get_model_module().get_list(**{'%s__exact' % f.name: getattr(self, f.name)}): |
993 | | os.remove(file_name) |
994 | | # Run any post-delete hooks. |
995 | | if hasattr(self, '_post_delete'): |
996 | | self._post_delete() |
| 1007 | db.db.commit() |
| 1008 | setattr(self, opts.pk.attname, None) |
| 1009 | for f in opts.fields: |
| 1010 | if isinstance(f, FileField) and getattr(self, f.attname): |
| 1011 | file_name = getattr(self, 'get_%s_filename' % f.name)() |
| 1012 | # If the file exists and no other object of this type references it, |
| 1013 | # delete it from the filesystem. |
| 1014 | if os.path.exists(file_name) and not opts.get_model_module().get_list(**{'%s__exact' % f.name: getattr(self, f.name)}): |
| 1015 | os.remove(file_name) |
| 1016 | # Run any post-delete hooks. |
| 1017 | if hasattr(self, '_post_delete'): |
| 1018 | self._post_delete() |