9

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.

Use-case:

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.

I have all the code written, it looks like the following (it's an example):

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 ;-)

My problem is I don't know where to write this code

Attempts:

So far, with Django 1.6.5, I came with some code from the init.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).

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.

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.

What would you recommend?

I'd be happy to provide more info if needed.

Thanks in advance,

Julien Greard
  • 969
  • 1
  • 12
  • 32
  • 1
    You might find something useful in [this stackoverflow question](http://stackoverflow.com/questions/2781383/where-to-put-django-startup-code). – Robin Mar 09 '16 at 08:05
  • I already saw it, the accepted answer of this link mentions the "ready()" method like I do. But according to Django doc, it's a bad practice to interract with models in this method... – Julien Greard Mar 09 '16 at 08:39
  • 3
    What does "startup" mean in this case? Usually you start a website once, and then it runs continually. Why do you need to populate tables at "startup", rather than eg in a migration? – Daniel Roseman Mar 09 '16 at 08:56
  • Well, my app has 2 inputs : one is the views (and the user using a web-browser), the other one is a web-service. I think (but maybe I am wrong), that migration and initial data stands for static data. In my case I want to use dynamic data for populating my table. – Julien Greard Mar 09 '16 at 09:12
  • 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. – Julien Greard Mar 09 '16 at 09:12
  • 2
    The way I did what you describe was with a [custom management command](https://docs.djangoproject.com/en/1.9/howto/custom-management-commands/). Not exactly sure how you would make sure it is ran on startup, but I used it to populate my database at intervals by running commands at set intervals through a task scheduler. – Robin Mar 09 '16 at 09:25
  • 1
    In my opinion your are looking for a [custom django-admin command](https://docs.djangoproject.com/en/1.9/howto/custom-management-commands/). Put this command on cron to be executed so often you need. – dani herrera Mar 09 '16 at 09:25
  • It looks like it's the good way to solve my pb thanks. I'll give it a try – Julien Greard Mar 09 '16 at 10:06
  • you can but one of the comment in the answer section, I'll accept it ;-) – Julien Greard Mar 10 '16 at 17:13

1 Answers1

1

Why you don't use Celery instead? I know you are asking about how to populate your Item table on start-up, but... I think a scheduled celery task here fits and solves in a natural way your problem.

trinchet
  • 6,753
  • 4
  • 37
  • 60
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/11771232) – Mike Cluck Mar 25 '16 at 17:20
  • @MikeC, I appreciate you comment, but the essential part of the answer is included: "a scheduled task", I also included the name of one of the tool we could use for solving: Celery, the link is just a shortcut to this tool. – trinchet Mar 25 '16 at 17:41
  • 1
    well Celery may be an option, but I think Django provides already ways to do what I needed without having to be dependant of celery. Indeed, the day I will use Celery to improve my program, then I will think about that but I don't want to be Celery dependant only for that. The management thing is what I need I think, so far it works – Julien Greard Mar 25 '16 at 17:41