Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some related objects that should not be deleted #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ MANIFEST
*.egg
docs/_build/
htmlcov/
.idea/
26 changes: 23 additions & 3 deletions pinax/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down