Skip to main content

Posts

Showing posts with the label initialize

Django: adding code execution on your app syncdb, or how to use Django Signals.

Hi there and let's talk about app initialization in Django. There are some cases when you want to initialize a Django app with creating some default values in database. In my case it was necessity to create default album in database to post user photos to. Sometimes you could just use get_or_create for those purposes. But it will be a good example if we will need something more complex in our app initialization; for e.g. generating thumbnails for photos or cleaning unused temporary files etc. So let's get started: Good place to put your initialization scripts is your app's __init__.py file. You can examine Djangoproject wiki for more info. Anyway here is my code for making this: from  django. db . models . signals   import  post_syncdb import  models from  models  import  Album   def  create_first_album ( sender,  ** kwargs ) :      """    Create your album sequence to create default album t...