Skip to main content

Tiny app to provide basic behaviors for django models.

Project description

django-basic-models-behaviors is a tiny app to provide basic behaviors like:

  • Timestampable

  • Publishable

  • SoftDeletable

  • Cacheable

Installation

Ok, so far: you’re crazy! But here is the way you can install this app:

$ pip install django-basic-models-behaviors

Usage

PublishableModel

Here is an example of Article using PublishableModel:

from basic_models_behaviors import models as behaviors
from django.db import models

class Article(behaviors.PublishableModel):
    title = models.CharField(max_length=255)
    contents = models.TextField()

Then:

>>> article = Article(title='Foo', contents='Lorem lipsum')
>>> article.publish()
>>> article.is_published()
True
>>> article.unpublish()
>>> article.is_published()
False
>>> article.is_not_published()
True

SoftDeletableModel

SoftDeletableModel behavior will add deleted_at field in set the current timestamp instead of delete the object. force_delete() will actually delete the model.

In your models.py:

from basic_models_behaviors import models as behaviors
from django.db import models

class Article(behaviors.SoftDeletableModel):
    title = models.CharField(max_length=255)
    contents = models.TextField()

Then:

>>> from models import Article
>>> article = Article(title='foo', contents='Lorem lipsum')
>>> article.delete()
>>> article.has_been_deleted()
True
>>> article.undelete()
>>> article.has_been_deleted()
False
>>> article.force_delete()

Tests

Run tests:

$ cd tests && python manage.py test

Authors

Nicolas Dubois <nicolas.c.dubois@gmail.com>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-basic-models-behaviors-0.5.0.tar.gz (7.9 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page