Re: Cascade Soft Delete
On Thu, Jul 23, 2009 at 2:57 AM, Steven Stelmach wrote: > > Hi all, > > I'm working with models (for a newspaper site) similar to this: > > - Abstract class Content with child class Article. > - Each Content object is related one-to-one with a ContentGeneric, > which has a published_status field. > - Articles have related_content, which is a M2M field with > ContentGeneric. > > I've implemented a soft delete on ContentGeneric by overriding the > get_queryset method on my manager to only fetch content with active > published_statuses, but when I ask an article for its related_content > via the ORM, references to soft deleted content are obviously still > valid and are returned. > > My question is basically: is there a way to make a default m2m manager > that fetches only active content? I'd like to be able to do something > like Articles.related_content.all() and have it return only active > related_content content instances. ... and just after I posted, my brain kicked into gear and thought of the obvious solution. Instead of trying to filter Article.related_content.all(), modify the default manager so that you can invoke Articles.related_content.active(). To do this, define a custom default manager for Content that exposes an 'active()' modifier. Rather than affecting all queries (by overriding get_query_set()), you add an 'active()' operator that returns a filtered query set. With this approach, both Articles.related_content.active() and ContentGeneric.objects.active() will both work. Yours Russ Magee %-) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Cascade Soft Delete
On Thu, Jul 23, 2009 at 2:57 AM, Steven Stelmach wrote: > > Hi all, > > I'm working with models (for a newspaper site) similar to this: > > - Abstract class Content with child class Article. > - Each Content object is related one-to-one with a ContentGeneric, > which has a published_status field. > - Articles have related_content, which is a M2M field with > ContentGeneric. > > I've implemented a soft delete on ContentGeneric by overriding the > get_queryset method on my manager to only fetch content with active > published_statuses, but when I ask an article for its related_content > via the ORM, references to soft deleted content are obviously still > valid and are returned. > > My question is basically: is there a way to make a default m2m manager > that fetches only active content? I'd like to be able to do something > like Articles.related_content.all() and have it return only active > related_content content instances. You can do this by setting the default manager on the related table, but this will affect all queries on the related table, not just the m2m queries. When you do an m2m query, Django uses the default manager on the related model to construct the query - that is, if A and B have an m2m relation, a query on A.b_set.all() uses B's default manager to retrieve results. If B's default manager only returns active content, then so will the m2m query. However, this then means that the default manager on B will only return active content, so explicit requests on B will be filtered by default. There isn't any way to specify the manager to use only in an m2m relation - either as a model configuration item or a query parameter. Adding this capability is something that has been proposed in the past; to my recollection, the sticking point has been finding an elegant way to express this configuration. Yours, Russ Magee %-) --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Cascade Soft Delete
Hi all, I'm working with models (for a newspaper site) similar to this: - Abstract class Content with child class Article. - Each Content object is related one-to-one with a ContentGeneric, which has a published_status field. - Articles have related_content, which is a M2M field with ContentGeneric. I've implemented a soft delete on ContentGeneric by overriding the get_queryset method on my manager to only fetch content with active published_statuses, but when I ask an article for its related_content via the ORM, references to soft deleted content are obviously still valid and are returned. My question is basically: is there a way to make a default m2m manager that fetches only active content? I'd like to be able to do something like Articles.related_content.all() and have it return only active related_content content instances. Thanks, Steve --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---