Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # settings.settings.py
- INSTALLED_APPS = localsettings.INSTALLED_APPS + (
- ...
- 'apps.myapp',
- )
- MIDDLEWARE_CLASSES = (
- 'apps.myapp.initialize.MyAppConfig',
- ...
- )
- # apps.myapp.models.py
- from django.db import models
- class Game(models.Model):
- name = models.CharField(max_length=30)
- plays = models.PositiveIntegerField()
- # apps.myapp.initialize.py
- from django.core.exceptions import MiddlewareNotUsed
- from .models import Game
- class MyAppConfig(object):
- name = 'apps.myapp'
- run_already = False
- def __init__(self):
- if not self.run_already:
- try:
- # If you want to call Save() and emit all its signals:
- for game in Game.objects.all():
- game.plays = 0
- game.save()
- # Or, if you want to update without calling any signals:
- Game.objects.update(plays=0)
- except Exception, err:
- print err
- pass
- self.run_already = True
- raise MiddlewareNotUsed('Startup complete')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement