模型管理器的可链接方法
django-chainable-manager的Python项目详细描述
允许链接自定义模型管理器方法,而不必跳过 奎丽斯特环。
安装
只需使用pip:
pip install django-chainable-manager
使用
创建扩展chainablemanager.ChainableManager:
的管理器fromdjango.dbimportmodelsfromchainablemanager.managerimportChainableManagerclassPostManager(ChainableManager):classQuerySetMixin(object):defpublished(self):returnself.filter(publish_date__gte=datetime.date.today())defwritten_by(self,user):returnself.filter(author=user)classPost(models.Model):title=models.CharField(max_length=255)content=models.TextField()publish_date=models.DateField()author=models.ForeignKey('auth.User')objects=PostManager()
现在在任何需要的地方使用它:
from.modelsimportPostmy_published_posts=Post.objects.written_by(request.user).published()