On Thu, Jul 23, 2009 at 2:57 AM, Steven Stelmach<sstelm...@gmail.com> 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
-~----------~----~----~----~------~----~------~--~---

Reply via email to