小巧的应用程序,为django模型提供基本行为。
项目描述
小巧的应用程序,为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