Django

Code

Scaffold Script

Looks like this uses old Forms, and is replaced by FormGenScript

We all know that "Every form is different" but sometimes they're quite similar. This script takes the names of an app and a model and spews out a simple set of form fields which you can then change as appropriate.

Usage is simple. Save the script next to manage.py in your project's directory and do something like this: ./scaffold.py -a ToDo -m Category

This is an interesting script to combine with ManipulatorScript

Enjoy

Example

From this:

class Category(meta.Model):
    category = meta.CharField(maxlength=50, unique=True)
    createdOn = meta.DateField(auto_now_add=True)
    modifiedOn = meta.DateField(auto_now=True)
    test = meta.ManyToManyField(auth.User, verbose_name='This is a test field', related_name='test')
    test2 = meta.OneToOneField(auth.User, verbose_name='test2', related_name='test2')

    def __repr__(self):
        return "%s" % (self.category,)

    class META:
        admin = meta.Admin()

To this:

<form method="POST" action=".">
  <p>
    <label for="id_category">category:</label>
    {{ form.category }}
    {% if form.category.errors %}
      *** {{ form.category.errors|join:", " }}
    {% endif %}
  </p>
  <p>
    <label for="id_createdOn">createdOn:</label>
    {{ form.createdOn }}
    {% if form.createdOn.errors %}
      *** {{ form.createdOn.errors|join:", " }}
    {% endif %}
  </p>
  <p>
    <label for="id_modifiedOn">modifiedOn:</label>
    {{ form.modifiedOn }}
    {% if form.modifiedOn.errors %}
      *** {{ form.modifiedOn.errors|join:", " }}
    {% endif %}
  </p>
  <p>
    <label for="id_test2">test2:</label>
    {{ form.test2 }}
    {% if form.test2.errors %}
      *** {{ form.test2.errors|join:", " }}
    {% endif %}
  </p>
  <p>
    <label for="id_test">This is a test field:</label>
    {{ form.test }}
    {% if form.test.errors %}
      *** {{ form.test.errors|join:", " }}
    {% endif %}
  </p>
<input type="submit" value="submit" />
</form>

TODO: shebang line should probably be #!/usr/bin/env python as in manage.py etc.

Attachments