A simple markup and processor to make initial models.py less tedious
Because programmers are lazy…
Modelish is a quick attempt to save a few keystrokes. Many data modeling tools are a GUI nightmare, with dropdowns and lots of buttons. But sometimes, especially when you are sketching something out, writing out full Django models can be a bit tedious.
Modelish will parse a file with a simplified model declaration in a flavor of YAML and generate Python code for model definition.
Normally I would say source code generation is a “Bad Idea”, but really even in Python, Django models are a mostly declarative syntax
pip install django-modelish
You can then start banging out models like this
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
You then just use the CLI tool:
This will generate code like this:
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()
The syntax is pretty simple:
<ModelName>: [doc: docstring] <fieldname>[-type-alias]: [type: type-alias], [args: arglist], kwarg: value
The field type is represented by a shortened ‘type’ alias so ‘char’ becomes ‘models.CharField’ etc (see grammarish below).
The type can be specified one of two ways, either following the field name with a hyphen, or explicitly declared as a type in the field definition.
Positional args to the model specified as an explicit list in square brackets, or as a comma delimited string.
The remaining block of the field definition consists of kwarg/value pairs.
Working with grammarish
Modelish works with a grammar composed of type aliases and defaults. The standard types are:
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
For each type - a set of default kwargs is defined in the grammar as defaults:
defaults: bool: default: true char: max_length: 100 blank: true nbool: null: true timestamp: auto-now: true
This default grammar can be replace, or enhanced by passing your own yaml files to the command. Use --grammar to replace the default grammer, and use --extra-grammar to merge in and update the default grammer with your own additions or changes.
That’s it - this isn’t meant to be any sort of full featured model builder or data modeler, it is just a simple DSLish bootstrap tool to give you a models.py starting point with a little less typing.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size django-modelish-0.1.tar.gz (9.4 kB)||File type Source||Python version None||Upload date||Hashes View|