diff --git a/.gitignore b/.gitignore index 544df4f..81df32d 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ MANIFEST *.egg docs/_build/ htmlcov/ +.idea/ \ No newline at end of file diff --git a/pinax/models/models.py b/pinax/models/models.py index f219649..ef4c16e 100644 --- a/pinax/models/models.py +++ b/pinax/models/models.py @@ -23,12 +23,32 @@ def active(self): def delete(self): # Fetch related models to_delete = get_related_objects(self) + # Couldn't find a better way to test if itertools.chain is empty + something_to_delete = False for obj in to_delete: - obj.delete() + something_to_delete = True + field_nullable = False + for field in obj._meta.fields: + internal_type = field.get_internal_type() + if internal_type == "ForeignKey" or internal_type == "OneToOneField": + if field.rel.to == self.__class__ and field.null: + # Set the foreignkey to None + setattr(obj, field.attname, None) + obj.save() + field_nullable = True + # Jump to the next iteration + continue + + if not field_nullable: + # Only soft delete the object if there is no foreignkey field + # with null=True + obj.delete() + self.date_removed = timezone.now() + + if not something_to_delete: + self.date_removed = timezone.now() - # Soft delete the object - self.date_removed = timezone.now() self.save() class Meta: