一个简单的标记和处理器,可以使初始models.py编写更加简单
项目描述
因为程序员都是懒惰的...
Modelish是一个节省几个键位的快速尝试。许多数据建模工具都是图形用户界面噩梦,有下拉菜单和很多按钮。但有时,尤其是在你绘制草图时,编写完整的Django模型可能会有些繁琐。
Modelish将解析具有简化模型声明的YAML文件,并生成用于模型定义的Python代码。
通常我会说源代码生成是一个“坏主意”,但实际上,即使在Python中,Django模型也主要是声明性语法
快速入门
安装django-predicate
pip install django-modelish
然后你可以开始像这样编写模型
Poll:
doc: This is the poll model
question:
type: char
max_length: 200
pub_date:
type: datetime
args: date published
Choice:
poll:
type: fk
args: [Poll]
choice_text-char:
max_length: 200
votes:
type: int
然后你只需使用CLI工具
modelish path-to-file.yml
这将生成如下代码
class Poll(models.Model):
"""This is the poll model"""
pub_date = models.DateTimeField(
'date published')
question = models.CharField(
max_length=200,
blank=True)
class Choice(models.Model):
choice_text = models.CharField(
max_length=200,
blank=True)
poll = models.ForeignKey(
'Poll')
votes = models.IntegerField()
语法
语法很简单
<ModelName>: [doc: docstring] <fieldname>[-type-alias]: [type: type-alias], [args: arglist], kwarg: value
字段类型由简短的“类型”别名表示,例如“char”变为“models.CharField”等(请参阅以下语法ish)。
类型可以通过两种方式指定,要么在字段名后跟一个连字符,要么在字段定义中显式声明为“类型”。
模型指定的位置参数以显式列表的形式放在方括号中,或以逗号分隔的字符串形式。
字段定义的剩余部分由kwarg/value对组成。
使用语法ish进行工作
Modelish使用由类型别名和默认值组成的语法。标准类型有
auto: AutoField
bigint: BigIntegerField
bool: BooleanField
char: CharField
date: DateField
datetime: DateTimeField
decimal: DecimalField
email: EmailField
file: FileField
float: FloatField
image: ImageField
int: IntegerField
ip: IPAddressField
gip: GenericIPAddressField
nbool: NullBooleanField
pint: PositiveIntegerField
psint: PositiveSmallIntegerField
slug: SlugField
sint: SmallIntegerField
text: TextField
time: TimeField
url: URLField
fk: ForeignKey
m2m: ManyToManyField
timestamp: DateTimeField
对于每种类型,在语法中定义了一组默认的kwargs作为默认值
defaults:
bool:
default: true
char:
max_length: 100
blank: true
nbool:
null: true
timestamp:
auto-now: true
这个默认的语法可以被替换或增强,通过向命令传递自己的yaml文件。使用--grammar来替换默认语法,并使用--extra-grammar来合并并更新默认语法,包括您的添加或更改。
就这些 - 这不是任何一种完整的模型构建器或数据建模工具,它只是一个简单的DSLish引导工具,可以帮助您以更少的输入创建models.py的起点。
项目详情
关闭
django-modelish-0.1.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | a7ffd8bdef4d0ac078901fce2d7c88ca10bb7a92e5d2af9f056c67b2b2838990 |
|
MD5 | 220c1f4d5e917d09778debeeda85cd45 |
|
BLAKE2b-256 | 95acd6246b9cf6da0f26abcab24eceacc2b00c4144a790d674f9976ef758a7ed |