Proudly ProcrasDonating

Technology Thoughts

Django Tricks, Part 2 – Initialize and Install

Welcome to Proudly ProcrasDonating :-)
Subscribe to my RSS feed and
visit ProcrasDonate.com

This post is the second in a series on Django tricks.

We started with automating Admin Model creation. Continuing with automation, this post will cover model initialization and installation.

Initialization is the stuff we would normally put in a constructor, such as register pre-save and post-save signals.

Models are meta-classes, so they don’t have normal constructors. Instead, we put all the initialization stuff in a classmethod called Initialize:

class MyModel(models.Model):     @classmethod     def Initialize(klass):         models.signals.pre_save.connect(MyModel.sanitize_user_input, sender=MyModel)

In the app’s __init__.py file, we automatically look for Initialize methods to call.

__init__.py

from models import ALL_MODELS for model in ALL_MODELS:     if hasattr(model, 'Initialize'):         model.Initialize()

We could put signal registration directly in the model’s file, but it’s nicer to encapsulate all this Initialization stuff in a function, and then automate that function’s execution.

For more neat initializations read part 3 of this series, Model mixins.

Installation is stuff we want to do once the model class is synched with the database, such as installing a fixture or initial data.

As with Initialization, we can optionally add a classmethod called install to a model. A post_syncdb function will look for install functions to call when iterating over models.

management.py

from django.dispatch import dispatcher from django.db.models import signals   import models   """ @summary: Install initial data when models are first created.   Automatically called by django. Find out more be searching for "management.py" and "Extra special stuff" in http://www.b-list.org/weblog/2006/sep/10/django-tips-laying-out-application/ document on wiki: http://bilumi.org/trac/wiki/PostSyncdbInstall """   def post_syncdb(signal, sender, app, created_models, **kwargs):     # only run when models we care about are first created     if signal == signals.post_syncdb and app == models:         for model in models.ALL_MODELS:             if hasattr(model, 'install'):                 if kwargs['verbosity'] > 0:                     print "Installing ", model                 model.install()   signals.post_syncdb.connect(post_syncdb)

Proudly ProcrasDonating,

Lucy.

3 Comments »

[...] started with automating Admin Model creation, and continued with automating initialization and installation, model mixins and [...]

[...] ProcrasDonating Technology Thoughts «Django Tricks, Part 2 – Initialize and Install Django Tricks, Part 4 – [...]

[...] started with automating Admin Model creation, and continued with automating initialization and installation, model mixins, forms and app [...]


Your comment

HTML-Tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>