Django stackstore模型
django-stackstore-model的Python项目详细描述
Django Staskstore模型
django-staskstore-model
是一个可以在最少的步骤中支持模型版本控制的库。在
- 支持Django 2和3
- 支持Python3.7、3.8(可能是2.7。未测试)
- 支持管理扩展。在
使用
型号
# models.py
from stackstore.models import AbstractStackModel
class MySampleModel(AbstractStackModel):
title = models.CharField(
max_length=100,
null=False,
blank=False,
)
body = models.TextField(
max_length=1000,
null=False,
blank=False,
)
Python壳
^{pr2}$管理扩展
筛选以仅显示对象的最新版本
仅显示具有相同堆栈组的项目
美国石油学会
查询集和经理
来自_stack_group的最新版本
返回每个堆栈组中最新对象的查询集。在
# Create instance
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> instance_lunch = MySampleModel.objects.create(title="Today's lunch", body="Spam with Beacon") # Create <MySampleModel: MySampleModel object (2)>
# Save each instance
>>> instance_breakfast.body = "Fried Egg with bread"
>>> instance_breakfast.save() # Save method create new <MySampleModel: MySampleModel object (3)>
>>> instance_lunch.body = "Meat Pasta"
>>> instance_lunch.save() # Save method create new <MySampleModel: MySampleModel object (4)>
>>> MySampleModel.objects.latest_from_stack_group() # You can fetch 3 & 4 My Sample Model
<StackStoreQuerySet [<MySampleModel: MySampleModel object (3)>, <MySampleModel: MySampleModel object (4)>]>
从堆栈组获取最新版本
返回与每个堆栈组中最新一组对象中的排序规则参数匹配的对象。在
# Create instance
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> instance_lunch = MySampleModel.objects.create(title="Today's lunch", body="Spam with Beacon") # Create <MySampleModel: MySampleModel object (2)>
# Save each instance
>>> instance_breakfast.body = "Fried Egg with bread"
>>> instance_breakfast.save() # Save method create new <MySampleModel: MySampleModel object (3)>
>>> MySampleModel.objects.get_latest_from_stack_group(body="Fried Egg with bread")
<MySampleModel: MySampleModel object (3)>
模型对象
保存
保存对象时,将创建一个新对象,并将其分配给与保存该对象相同的堆栈组_uuid。 这意味着将生成一个新版本。在
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> instance_breakfast.body = "Fried Egg with bread"
>>> instance_breakfast.save() # Save method create version object <MySampleModel: MySampleModel object (2)>
>>> MySampleModel.objects.all()
<StackStoreQuerySet [<MySampleModel: MySampleModel object (1)>, <MySampleModel: MySampleModel object (2)>]>
如果要保存覆盖而不创建新版本,则应在save
方法中将False
传递给__create_new_version
。在
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg")
>>> instance_breakfast.save(__create_new_version=False)
>>> instance_breakfast.pk
1
强制删除
取下物体。在
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> MySampleModel.objects.all()
<StackStoreQuerySet [<MySampleModel: MySampleModel object (1)>]>
>>> instance_breakfast.force_delete()
>>> MySampleModel.objects.all()
<StackStoreQuerySet []>
调用现有的delete
方法将引发NotImplementedError异常。在
同一组项目
返回一个QuerySet,它返回属于同一个Stackgroup的所有对象。在
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> [instance_breakfast.save() for i in range(2)]
>>> instance_breakfast.same_group_items()
<StackStoreQuerySet [<MySampleModel: MySampleModel object (3)>, <MySampleModel: MySampleModel object (2)>, <MySampleModel: MySampleModel object (1)>]>
上一个_实例
返回对象的早期版本。在
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> [instance_breakfast.save() for i in range(2)]
>>> second_gen_instance = instance_breakfast.same_group_items().order_by("pk")[1]
>>> second_gen_instance.previous_instance()
<MySampleModel: MySampleModel object (1)>
如果对象不存在,则引发DoesNotExist异常。在
下一个实例
返回对象的下一个版本。在
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> [instance_breakfast.save() for i in range(2)]
>>> second_gen_instance = instance_breakfast.same_group_items()[1]
>>> second_gen_instance.next_instance()
<MySampleModel: MySampleModel object (3)>
如果对象不存在,则引发DoesNotExist异常。在
最新的_实例
返回其自身对象所属的Stackgroup的最新对象。在
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> [instance_breakfast.save() for i in range(2)]
>>> first_gen_instance = instance_breakfast.same_group_items().order_by("pk")[0]
>>> first_gen_instance.latest_instance()
<MySampleModel: MySampleModel object (3)>
如果对象不存在,则引发DoesNotExist异常。在
最早的_实例
返回堆栈组中其自身对象所属的最旧对象。在
>>> instance_breakfast = MySampleModel.objects.create(title="Today's breakfast", body="Spam with Egg") # Create <MySampleModel: MySampleModel object (1)>
>>> [instance_breakfast.save() for i in range(2)]
>>> third_gen_instance = instance_breakfast.same_group_items().order_by("pk")[2]
>>> third_gen_instance.earliest_instance()
<MySampleModel: MySampleModel object (1)>
如果对象不存在,则引发DoesNotExist异常。在
演示
提供Docker配置。请使用以下命令启动它。启动完成后转到http://localhost:8000。在
$ docker-compose up
登录帐户是ID:admin
密码:admin
许可证
麻省理工学院执照
- 项目
标签: