title: Django - execute code at start-up
body: <p>I am using Django 1.9.3. I have a project with several apps. I would like to update the tables of one of the app at the startup of the project.</p>
  
  <p><strong>Use-case:</strong></p>
  
  <p>For example, let's say I want to sell items on my website. I have an app which contains the model Item. I have a web-service outside Django which provide the service "give_all_items_available()". I want to provide to my user the list of items using the web-site. So I think I have to update my database regularly (at start-up and every once in a while) with that web-service input.</p>
  
  <p>I have all the code written, it looks like the following (it's an example):</p>
  
  <pre><code>from my_app.models import My_table
  
  def on_startup():
       my_thread = Thread(execute = populate_tables, loopmode = True, background = True) # thread running in loopmode in background
       my_thread.start() # starts the thread and returns
  
  def populate_tables()
       response = call_webservice() # let's imagine this method returns data for creating a new model instance
       My_table(response).save() # this save() isn't threadsafe in this example, but that's not my point ;-)
  </code></pre>
  
  <p>My problem is I don't know where to write this code</p>
  
  <p><strong>Attempts:</strong></p>
  
  <p>So far, with Django 1.6.5, I came with some code from the <strong>init</strong>.py file of my app. It was working, but I thought it was quite ugly (starting a thread with an "import" looks really like hidden code).</p>
  
  <p>I saw in Django 1.9 the "ready()" method. But it's written in the documentation to not deal with models in this method so I am confused.</p>
  
  <p>I could add the startup code in the command starting my server but this startup code is app oriented and in my opinion, the projects has nothing to do with it.</p>
  
  <p>What would you recommend?</p>
  
  <p>I'd be happy to provide more info if needed.</p>
  
  <p>Thanks in advance,</p>