File: model_extensions.rst

package info (click to toggle)
python-django-extensions 4.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,820 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (70 lines) | stat: -rw-r--r-- 2,528 bytes parent folder | download | duplicates (3)
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
Model Extensions
================

:synopsis: Model Extensions

Introduction
------------

Django Extensions provides you a set of Abstract Base Classes for models
that implements commonly used patterns like holding the model's creation
and last modification dates.

Database Model Extensions
-------------------------

* *ActivatorModel* - Abstract Base Class that provides a ``status``,
  ``activate_date``, and ``deactivate_date`` fields.

The ``status`` field is an ``IntegerField`` whose value is chosen from a tuple
of choices - active and inactive - defaulting to active. This model also
exposes a custom manager, allowing the user to easily query for active or
inactive objects.

E.g.: ``Model.objects.active()`` returns all instances of ``Model`` that have an
active status.

* *TitleDescriptionModel* - This Abstract Base Class model provides ``title`` and ``description`` fields.

The ``title`` field is ``CharField`` with a maximum length of 255 characters,
non-nullable. ``description``. On the other hand, ``description`` is a
nullable ``TextField``.

* *TimeStampedModel* - An Abstract Base Class model that provides self-managed
  ``created`` and ``modified`` fields.

Both of the fields are customly defined in Django Extensions as
``CreationDateTimeField`` and ``ModificationDateTimeField``.
Those fields are subclasses of Django's ``DateTimeField`` and will store
the value of ``django.utils.timezone.now()`` on the model's creation
and modification, respectively

* *TitleSlugDescriptionModel* - An Abstract Base Class model that, like the
  ``TitleDescriptionModel``, provides ``title`` and ``description`` fields
  but also provides a self-managed ``slug`` field which populates from the title.

That field's class is a custom defined `AutoSlugField <field_extensions.html>`_, based on Django's
``SlugField``. By default, it uses ``-`` as a separator, is unique and does
not accept blank values.
It is possible to customize ``slugify_function``
by defining your custom function within a model:

.. code-block:: python

    # models.py

    from django.db import models

    from django_extensions.db.models import TitleSlugDescriptionModel


    class MyModel(TitleSlugDescriptionModel, models.Model):

        def slugify_function(self, content):
            """
            This function will be used to slugify
            the title (default `populate_from` field)
            """
            return content.replace('_', '-').lower()

See `AutoSlugField docs <field_extensions.html>`_ for more details.