1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
Models
======
TimeFramedModel
---------------
An abstract base class for any model that expresses a time-range. Adds
``start`` and ``end`` nullable DateTimeFields, and provides a new
``timeframed`` manager on the subclass whose queryset pre-filters results
to only include those which have a ``start`` which is not in the future,
and an ``end`` which is not in the past. If either ``start`` or ``end`` is
``null``, the manager will include it.
.. code-block:: python
from model_utils.models import TimeFramedModel
from datetime import datetime, timedelta
class Post(TimeFramedModel):
pass
p = Post()
p.start = datetime.utcnow() - timedelta(days=1)
p.end = datetime.utcnow() + timedelta(days=7)
p.save()
# this query will return the above Post instance:
Post.timeframed.all()
p.start = None
p.end = None
p.save()
# this query will also return the above Post instance, because
# the `start` and/or `end` are NULL.
Post.timeframed.all()
p.start = datetime.utcnow() + timedelta(days=7)
p.save()
# this query will NOT return our Post instance, because
# the start date is in the future.
Post.timeframed.all()
TimeStampedModel
----------------
This abstract base class just provides self-updating ``created`` and
``modified`` fields on any model that inherits from it.
StatusModel
-----------
Pulls together :ref:`StatusField`, :ref:`MonitorField` and :ref:`QueryManager`
into an abstract base class for any model with a "status."
Just provide a ``STATUS`` class-attribute (a :ref:`Choices` object or a
list of two-tuples), and your model will have a ``status`` field with
those choices, a ``status_changed`` field containing the date-time the
``status`` was last changed, and a manager for each status that
returns objects with that status only:
.. code-block:: python
from model_utils.models import StatusModel
from model_utils import Choices
class Article(StatusModel):
STATUS = Choices('draft', 'published')
# ...
a = Article()
a.status = Article.STATUS.published
# this save will update a.status_changed
a.save()
# this query will only return published articles:
Article.published.all()
SoftDeletableModel
------------------
This abstract base class just provides a field ``is_removed`` which is
set to True instead of removing the instance. Entities returned in
manager ``available_objects`` are limited to not-deleted instances.
Note that relying on the default ``objects`` manager to filter out not-deleted
instances is deprecated. ``objects`` will include deleted objects in a future
release.
UUIDModel
------------------
This abstract base class provides ``id`` field on any model that inherits from it
which will be the primary key.
If you dont want to set ``id`` as primary key or change the field name, you can override it
with our `UUIDField`_
Also you can override the default uuid version. Versions 1,3,4 and 5 are now supported.
.. code-block:: python
from model_utils.models import UUIDModel
class MyAppModel(UUIDModel):
pass
.. _`UUIDField`: https://github.com/jazzband/django-model-utils/blob/master/docs/fields.rst#uuidfield
SaveSignalHandlingModel
-----------------------
An abstract base class model to pass a parameter ``signals_to_disable``
to ``save`` method in order to disable signals
.. code-block:: python
from model_utils.models import SaveSignalHandlingModel
class SaveSignalTestModel(SaveSignalHandlingModel):
name = models.CharField(max_length=20)
obj = SaveSignalTestModel(name='Test')
# Note: If you use `Model.objects.create`, the signals can't be disabled
obj.save(signals_to_disable=['pre_save'] # disable `pre_save` signal
|