So i decided to fork pinax-models because i need the capability of soft
deleting objects and their related (setting a flag but not really deleting
them). This is a project that seems to be unmaintained , so i diged into
the code and i am trying to make some improvements.
It basically fetched the objects, looks for its related and then saves the
date_removed to fake the deletion.
It still has some issues and one of them which i am trying to solve is that
it always "cascades" the deletes of the related but it should only do it if
the related field is not nullable. if its nullable then it should not
delete the related field.
I have 2 question regarding this matter ( i am using Django 1.7):
1- How can i improve this code below to support that feature? I know it has
something to do with _meta, thought about using
`obj._meta.get_fields_with_model` but i am afraid it does not do
`options.concrete_fields
+ options.many_to_many + options.virtual_fields`. i was thinking about
getting all the existing fields in a model an then evaluate if its a
foreign key to a certain model class and then check if
`obj._meta.get_field('something').null`. Is this the right approach??
2 - The original author is using self._meta.get_all_related_objects() but
in the _metadocumentation i saw other functions like for example
get_all_related_many_to_many_objectsso do i need to add that validation to
the code as well or self._meta.get_all_related_objects() is enough ??
I am trying to improve this and push it to my pinax-models fork so any help
would be appreciated.
code:
class CustomLogicalDeleteModel(LogicalDeleteModel):
def delete(self):
# Fetch related models
related_objs = [
relation.get_accessor_name()
for relation in self._meta.get_all_related_objects()
]
for objs_model in related_objs:
if hasattr(self, objs_model):
local_objs_model = getattr(self, objs_model)
if hasattr(local_objs_model, 'all'):
# Retrieve all related objects
objs = local_objs_model.all()
for obj in objs:
# Checking if inherits from logicaldelete
if not issubclass(obj.__class__, LogicalDeleteModel):
break
obj.delete()
else:
obj = local_objs_model
if not issubclass(obj.__class__, LogicalDeleteModel):
break
obj.delete()
# Soft delete the object
self.date_removed = datetime.datetime.now()
self.save()
class Meta:
abstract = True
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/52a0b2b6-2fb6-44ad-9ab9-fd05613c1622%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.