django的方便常量字段
django-konst的Python项目详细描述
django konst
django konst
django-konst
是django的一个实用程序,它使定义、使用和存储基于整数和字符串的常量变得简单易读。它避免了常数的传递
使计算简洁易读。
它还使通过表单和drf序列化程序公开这些常量变得简单。
定义
常数可以用友好的名称定义,后面是整数或文本。
常数
fromkonstimportConstant# states backed by integersstates=Constants(Constant(pending=0),Constant(active=1),Constant(inactive=2))# and another set of constants backed by stringscolours=Constants(Constant(red="FF0000"),Constant(green="00FF00"),Constant(yellow="FFFF80"),Constant(white="FFFFFF"))
常数组
有时,有必要对常数进行分组并测试该组中的成员资格。
为此,django-konst
提供了一个ConstantGroup
类。
fromkonstimportConstant,ConstantGroup# states backed by integersstates=Constants(Constant(active=0),Constant(cancelled_ontime=1),Constant(cancelled_late=2),ConstantGroup("cancelled",("cancelled_ontime","cancelled_late")))
在模型中
虽然不是严格必要的,但建议有效地命名常量 通过在模型定义的范围内定义它们。这意味着你有你的常数 无论您在何处拥有模型类,以及任何模型实例。
fromdjango.dbimportmodelsfromdjango.utils.translationimportugettext_lazyas_fromkonstimportConstant,ConstantGroup,Constantsfromkonst.models.fieldsimport(ConstantChoiceCharField,ConstantChoiceField)classApple(models.Model):purposes=Constants(Constant(cooking=0,label=_("Cook me!")),Constant(eating=1,label=_("Eat me!")),Constant(juicing=2,label=_("Juice me!")),Constant(ornamental=3,label=_("Just look how pretty I am!")),ConstantGroup("culinary",("cooking","eating","juicing")))colours=Constants(Constant(red="FF0000",label=_("red")),Constant(green="00FF00",label=_("green")),Constant(yellow="FFFF80",label=_("yellow")),Constant(white="FFFFFF",label=_("white")),)name=models.CharField(max_length=30)purpose=ConstantChoiceField(constants=purposes)colour=ConstantChoiceCharField(constants=colours,max_length=30)
使用
这个库的全部目的是使用以这种方式定义的常量 简单明了。
代码中的apple=Apple.objects.get(name='Granny Smith')apple.purpose.cookingTrueapple.colour.redTrueapple.colour.greenFalse# we don't care about the specific purpose, just whether it is as food# or not, so use the ConstantGroup!apple.purpose.culinaryTrue
在模板中
{% if apple.purpose.eating %}
You should bite this {{ apple.name }}!
{% endif %}
使用django的orm
red_apples=Apple.objects.filter(colour=Apple.colours.red)culinary_apples=Apple.objects.filter(purpose__in=Apple.purposes.culinary)
使用django rest框架
{% if apple.purpose.eating %}
You should bite this {{ apple.name }}!
{% endif %}
使用django的orm
red_apples=Apple.objects.filter(colour=Apple.colours.red)culinary_apples=Apple.objects.filter(purpose__in=Apple.purposes.culinary)
使用django rest框架
使用konst.extras.drf.fields.ConstantChoiceField
序列化程序字段
django rest框架可以输出和接收常量值。
fromkonst.extras.drf.fieldsimportConstantChoiceFieldfromrest_frameworkimportserializersclassAppleSerializer(serializers.ModelSerializer):purpose=ConstantChoiceField(Apple.purposes)colour=ConstantChoiceField(Apple.colours)classMeta:model=Applefields=("name","purpose","colour")# let's see how it handles bad valuesserializer=AppleSerializer(data={"name":"Fuji","colour":"blue","purpose":"dicing"})serializer.is_valid()Falseserializer.errors{'colour':[u'"blue" is not a valid choice.'],'purpose':[u'"dicing" is not a valid choice.']}# and now how it handles some good valuesserializer=AppleSerializer(data={"name":"Fuji","colour":"red","purpose":"eating"})serializer.is_valid()True# let's create a database entry!instance=serializer.save()# and now our instance can be interacted with neatlyinstance.colour.redTrue# finally, let's see how this looks when rendering JSONAppleSerializer(instance=instance).data{"name":"Fuji","colour":"red","purpose":"eating"}
贡献
django-konst
支持多种python和django版本。最好在提交之前测试每一个。我们的Circle CI Integration将在推送时测试这些内容,但在提交之前就知道不需要执行大量额外的提交才能通过构建。
环境设置
为了在所有这些python上轻松测试并运行ci将执行的完全相同的操作,您需要设置pyenv并安装tox.ini中概述的python版本。
如果您使用的是MacOSX,建议您使用brew。安装brew
后,运行:
$ brew install pyenv pyenv-virtualenv pyenv-virtualenvwrapper
然后:
pyenv install -s 2.7.14
pyenv install -s 3.4.7
pyenv install -s 3.5.4
pyenv install -s 3.6.3
pyenv virtualenv 2.7.14
pyenv virtualenv 3.4.7
pyenv virtualenv 3.5.4
pyenv virtualenv 3.6.3
pyenv global 2.7.14 3.4.7 3.5.4 3.6.3
pip install detox
运行测试套件:
确保您不在virtualenv
中,然后:
$ detox
这将按照tox.ini
中的定义并行执行测试矩阵。