Mixing model querysets

How to make use of them

The querysets represented herein are designed to be used with their appropriate abstract models, and typically provide additional methods by which to filter objects.

The obvious way to get these yourself is to create your own subclass which implements them, and wire it up to a manager:

from helpfulfields.querysets import (ChangeTrackingQuerySet,
                                     DatePublishingQuerySet,
                                     SoftDeleteQuerySet)

class MyCustomQS(ChangeTrackingQuerySet, DatePublishingQuerySet, SoftDeleteQuerySet):
    pass

class MyCustomManager(Manager):
    def get_query_set(self):
        return MyCustomQS(self.model)

    def created_recently(self, minutes=30):
        # this method is exposed via ChangeTrackingQuerySet
        return self.get_query_set().created_recently(minutes=minutes)

   #[...]

class MyModel(Model):
    objects = MyCustomManager()

Avoiding the boilerplate

Better than doing it all yourself though, is just to use a PassThroughManager from django-model-utils to reduce the amount of repetition involved:

from model_utils.managers import PassThroughManager
from helpfulfields.querysets import (ChangeTrackingQuerySet,
                                     DatePublishingQuerySet,
                                     SoftDeleteQuerySet)

class MyCustomQS(ChangeTrackingQuerySet, DatePublishingQuerySet, SoftDeleteQuerySet):
    pass

class MyModel(Model):
    objects = PassThroughManager.for_queryset_class(MyCustomQS)()

The querysets available

class helpfulfields.querysets.ChangeTrackingQuerySet(model=None, query=None, using=None)[source]

A custom queryset for filtering models using the ChangeTracking model.

created_recently(**kwargs)[source]

Goes hand in hand with the created_recently() method. Finds all object instances created within the last N minutes.

Accepts a list of kwargs which are passed directly to timedelta; in the absence of any kwargs the timedelta is told 30 minutes is recent.

Returns:filtered objects
Return type:QuerySet subclass
modified_recently(**kwargs)[source]

Goes hand in hand with the modified_recently() method. Finds all object instances modified within the last N minutes.

Accepts a list of kwargs which are passed directly to timedelta; in the absence of any kwargs the timedelta is told 30 minutes is recent.

Returns:filtered objects
Return type:QuerySet subclass
class helpfulfields.querysets.DatePublishingQuerySet(model=None, query=None, using=None)[source]

A custom queryset for filtering things using the DatePublishing abtract model via the new fields it provides.

published()[source]

Find all objects whose unpublish_on value is in the future, or is null, and also have a publish_on value which is in the past or present.

Returns:All published objects
Return type:QuerySet subclass
unpublished()[source]

Find all objects whose unpublish_on value is in the past, or alternatively have a publish_on value which is still in the future.

Returns:All unpublished objects
Return type:QuerySet subclass
class helpfulfields.querysets.PublishingQuerySet(model=None, query=None, using=None)[source]

A custom queryset for filtering things using the Publishing abstract model via it’s boolean.

published()[source]

Find all objects who have have is_published set to True.

Returns:All published objects
Return type:QuerySet subclass
unpublished()[source]

Find all objects who have have is_published set to False.

Returns:All published objects
Return type:QuerySet subclass
class helpfulfields.querysets.SoftDeleteQuerySet(model=None, query=None, using=None)[source]

A custom queryset which goes hand in hand with the SoftDelete model to provide a way to filter by the additional field that creates.

Warning

This should not be relied on to prevent data loss, as it is very much an incomplete idea right now.

all()[source]

Finds all objects which haven’t been marked as deleted. This includes those which have been restored previously.

Returns:objects which haven’t been marked as deleted
Return type:QuerySet subclass
deleted()[source]

filters the queryset looking for deleted items only.

Returns:objects which are currently deleted
Return type:QuerySet subclass
restored()[source]

filters the queryset looking for objects which have been previously deleted, and since restored.

Returns:objects which were once deleted.
Return type:QuerySet subclass

Project Versions

Table Of Contents

Previous topic

Mixin models

Next topic

Modeladmin helpers

This Page