DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > A Data Migration for Every Django Project

A Data Migration for Every Django Project

David Winterbottom user avatar by
David Winterbottom
·
Jun. 19, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
3.54K Views

Join the DZone community and get the full member experience.

Join For Free

How to use a South data migration to avoid accidentally sending emails from example.com.

Problem

Consider the following snippet from Django's docs [1] for sending a confirmation email:

from django.contrib.sites.models import Site
from django.core.mail import send_mail

def register_for_newsletter(request):
    current_site = Site.objects.get_current()
    send_mail(
        'Thanks for subscribing to %s alerts' % current_site.name,
        'Thanks for your subscription. We appreciate it.\n\n-The %s team.' % current_site.name,
        'editor@%s' % current_site.domain,
        [user.email]
    )

Here the domain for the email sender is taken from the 'current site' instance, which is controlled by Django's 'Sites' framework and accessible by a custom method on the manager of the Site model.

By default, a Site instance is created with domain and display name 'example.com' and you have to correct these values. This is often done by hand using the admin suite.

However, as with any manual change, it's easy to forget and you'll often find Django projects sending email from editor@example.com. Highly embarassing.

Solution

Automation, of course! We can use a South data migration to set the domain and display name correctly in each environment.

First, ensure that each environment has settings for the domain and site name.

# conf/test.py
...
DOMAIN_NAME = 'test.project.client.tangentlabs.co.uk'
SITE_NAME = 'project - client (test)'

# conf/stage.py
...
DOMAIN_NAME = 'stage.project.client.tangentlabs.co.uk'
SITE_NAME = 'project - client (stage)'

This snippet assumes you are using a set-up similar to that outlined by David Cramer, where an environmental variable specifies an additional settings file to import. You don't have to use this method; employing a settings_local.py file for each environment works just as well.

Next, create a data migration to set the domain and display name correctly in each environment. This migration sits most naturally in the django.contrib.sites app, but since that's in Django's core, it's not an option. You could use an existing app within your project to house the migration or perhaps crhttp://www.dzone.com/node/add/articleeate a simple 'core' or 'data' app to house data migrations that alter 3rd party apps.

Since we're not using the actual app where the Site model is defined, we must employ South's --freeze option to ensure the Site model is available to the migration.

python manage.py datamigration <appname> create_domains --freeze=sites

Finally implement the forwards method:

from south.v2 import DataMigration
from django.conf import settings

class Migration(DataMigration):

    def forwards(self, orm):
        Site = orm['sites.Site']
        site = Site.objects.get(id=settings.SITE_ID)
        site.domain = settings.DOMAIN_NAME
        site.name = settings.SITE_NAME
        site.save()

Then your next deployment to each environment will perform the update.

Data migration Django (web framework) Data (computing) app Snippet (programming) IT House (operating system) Framework

Published at DZone with permission of David Winterbottom, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Right Way to Hybridize Your Product Development Technique
  • An Overview of 3 Java Embedded Databases
  • How to Configure Git in Eclipse IDE
  • How to Optimize MySQL Queries for Speed and Performance

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo