Steal code from the current Model.delete method.  It uses 
django.db.models.deletion.Collector to find the associated objects.  I would 
extend that to do what you want.

> On Aug 4, 2015, at 7:38 AM, psychok7 <[email protected]> wrote:
> 
> 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] 
> <mailto:[email protected]>.
> To post to this group, send email to [email protected] 
> <mailto:[email protected]>.
> Visit this group at http://groups.google.com/group/django-users 
> <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
>  
> <https://groups.google.com/d/msgid/django-users/52a0b2b6-2fb6-44ad-9ab9-fd05613c1622%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

Peter of the Norse
[email protected]



-- 
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/818DF3E6-1A1A-40B9-AB87-2BE50EC6587C%40radio1190.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to