跳转到主要内容

小巧的应用程序,为django模型提供基本行为。

项目描述

Latest Version on PyPI Supported Python versions TravisCI status

小巧的应用程序,为django模型提供基本行为,如

  • 时间戳

  • 可发布

  • 软删除

  • 缓存

安装

从PyPI

$ pip install django-basic-models-behaviors

使用

PublishableModel

以下是一个使用PublishableModel的Article示例

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()

然后

>>> 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行为将在设置当前时间戳而不是删除对象时添加deleted_at字段。force_delete()实际上会删除模型。

在你的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()

然后

>>> 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()

测试

运行测试

$ pip install -r tests/requirements.txt
$ py.test --ds=tests.settings tests

作者

项目详情


下载文件

为您的平台下载文件。如果您不确定要选择哪一个,请了解更多关于 安装包 的信息。

源分布

django-basic-models-behaviors-0.6.0.tar.gz (7.4 kB 查看散列)

上传时间:

由以下机构支持