Advertisement
Guest User

Untitled

a guest
May 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. # settings.settings.py
  2.  
  3. INSTALLED_APPS = localsettings.INSTALLED_APPS + (
  4. ...
  5. 'apps.myapp',
  6. )
  7.  
  8. MIDDLEWARE_CLASSES = (
  9. 'apps.myapp.initialize.MyAppConfig',
  10. ...
  11. )
  12.  
  13.  
  14. # apps.myapp.models.py
  15.  
  16. from django.db import models
  17.  
  18.  
  19. class Game(models.Model):
  20. name = models.CharField(max_length=30)
  21. plays = models.PositiveIntegerField()
  22.  
  23.  
  24. # apps.myapp.initialize.py
  25.  
  26. from django.core.exceptions import MiddlewareNotUsed
  27.  
  28. from .models import Game
  29.  
  30.  
  31. class MyAppConfig(object):
  32. name = 'apps.myapp'
  33. run_already = False
  34.  
  35. def __init__(self):
  36. if not self.run_already:
  37. try:
  38. # If you want to call Save() and emit all its signals:
  39. for game in Game.objects.all():
  40. game.plays = 0
  41. game.save()
  42.  
  43. # Or, if you want to update without calling any signals:
  44. Game.objects.update(plays=0)
  45.  
  46. except Exception, err:
  47. print err
  48. pass
  49.  
  50. self.run_already = True
  51. raise MiddlewareNotUsed('Startup complete')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement