Best way to update my django model coming from an external api source?












1















I am getting my data through requesting an api source, then I put it in my django model. However, data update daily.. so how can I update these data without rendering it everytime?



def index (request):
session = requests.Session()
df = session.get('https://api.coincap.io/v2/assets')
response= df.json()
coin = response['data']
final_result = coin.to_dict('records')
for coin in final_result:
obj, created = Coincap.objects.update_or_create(
symbol = coin['symbol'],
name = coin['name'],
defaults = {
'price': coin['priceUsd']
})
return render(request, '/home.html/')


Right now, I have to go to /home.html , if I want my data update. However, my goal is to later serialize it and make it REST api data, so I wouldn't touch django template anymore. Anyway for it to update internally once a day after i do manage.py runserver?










share|improve this question


















  • 1





    Seems like it should be a periodic task. You can use either a cronjob or celery to do the update.

    – ruddra
    Nov 14 '18 at 5:48






  • 1





    You can use crontab for this purpose and for Django you can use django-crontab to can call your views periodically.

    – Jagjeet Singh
    Nov 14 '18 at 6:24


















1















I am getting my data through requesting an api source, then I put it in my django model. However, data update daily.. so how can I update these data without rendering it everytime?



def index (request):
session = requests.Session()
df = session.get('https://api.coincap.io/v2/assets')
response= df.json()
coin = response['data']
final_result = coin.to_dict('records')
for coin in final_result:
obj, created = Coincap.objects.update_or_create(
symbol = coin['symbol'],
name = coin['name'],
defaults = {
'price': coin['priceUsd']
})
return render(request, '/home.html/')


Right now, I have to go to /home.html , if I want my data update. However, my goal is to later serialize it and make it REST api data, so I wouldn't touch django template anymore. Anyway for it to update internally once a day after i do manage.py runserver?










share|improve this question


















  • 1





    Seems like it should be a periodic task. You can use either a cronjob or celery to do the update.

    – ruddra
    Nov 14 '18 at 5:48






  • 1





    You can use crontab for this purpose and for Django you can use django-crontab to can call your views periodically.

    – Jagjeet Singh
    Nov 14 '18 at 6:24
















1












1








1








I am getting my data through requesting an api source, then I put it in my django model. However, data update daily.. so how can I update these data without rendering it everytime?



def index (request):
session = requests.Session()
df = session.get('https://api.coincap.io/v2/assets')
response= df.json()
coin = response['data']
final_result = coin.to_dict('records')
for coin in final_result:
obj, created = Coincap.objects.update_or_create(
symbol = coin['symbol'],
name = coin['name'],
defaults = {
'price': coin['priceUsd']
})
return render(request, '/home.html/')


Right now, I have to go to /home.html , if I want my data update. However, my goal is to later serialize it and make it REST api data, so I wouldn't touch django template anymore. Anyway for it to update internally once a day after i do manage.py runserver?










share|improve this question














I am getting my data through requesting an api source, then I put it in my django model. However, data update daily.. so how can I update these data without rendering it everytime?



def index (request):
session = requests.Session()
df = session.get('https://api.coincap.io/v2/assets')
response= df.json()
coin = response['data']
final_result = coin.to_dict('records')
for coin in final_result:
obj, created = Coincap.objects.update_or_create(
symbol = coin['symbol'],
name = coin['name'],
defaults = {
'price': coin['priceUsd']
})
return render(request, '/home.html/')


Right now, I have to go to /home.html , if I want my data update. However, my goal is to later serialize it and make it REST api data, so I wouldn't touch django template anymore. Anyway for it to update internally once a day after i do manage.py runserver?







django python-3.x django-rest-framework






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 5:01









MachiMachi

4117




4117








  • 1





    Seems like it should be a periodic task. You can use either a cronjob or celery to do the update.

    – ruddra
    Nov 14 '18 at 5:48






  • 1





    You can use crontab for this purpose and for Django you can use django-crontab to can call your views periodically.

    – Jagjeet Singh
    Nov 14 '18 at 6:24
















  • 1





    Seems like it should be a periodic task. You can use either a cronjob or celery to do the update.

    – ruddra
    Nov 14 '18 at 5:48






  • 1





    You can use crontab for this purpose and for Django you can use django-crontab to can call your views periodically.

    – Jagjeet Singh
    Nov 14 '18 at 6:24










1




1





Seems like it should be a periodic task. You can use either a cronjob or celery to do the update.

– ruddra
Nov 14 '18 at 5:48





Seems like it should be a periodic task. You can use either a cronjob or celery to do the update.

– ruddra
Nov 14 '18 at 5:48




1




1





You can use crontab for this purpose and for Django you can use django-crontab to can call your views periodically.

– Jagjeet Singh
Nov 14 '18 at 6:24







You can use crontab for this purpose and for Django you can use django-crontab to can call your views periodically.

– Jagjeet Singh
Nov 14 '18 at 6:24














2 Answers
2






active

oldest

votes


















2














One simple and common solution is to create a custom Django admin command and use Cron to run it at specified intervals. You can write a command's code to your liking and it can have access to all of the models, settings and other parts of your Django project.



You would put your code making a request and writing data to the DB, using your Django models, in your new Command class's handle() method (obviously request parameter is no longer needed). And then, if for example you have named your command update_some_data, you can run it as python manage.py update_some_data.



Assuming Cron exists and is running on the machine. Then you could setup Cron to run this command for you at specified intervals, for example create a file /etc/cron.d/your_app_name and put



0 4 * * * www-data /usr/local/bin/python /path/to/your/manage.py update_some_data >> /var/log/update_some_data.log  2>&1


This would make your update be done everyday at 04:00. If your command would provide any output, it will be written to /var/log/update_some_data.log file.
Of course this is just an example, so your server user running your app (www-data here) and path to the Python executable on the server (/usr/local/bin/python here) should be adjusted for particular use.



See links for further guidance.






share|improve this answer































    1














    For those that are looking for an example:



    from django.core.management.base import BaseCommand
    class Command(BaseCommand):
    def handle(self,*args,**kwargs):
    //Your request api here
    for coin in final_result:
    obj, created = Coincap.objects.update_or_create(
    symbol = coin['symbol'],
    name = coin['name'],
    defaults = {
    'price': coin['priceUsd']})


    Then you run in with cron just as Nikita suggested.






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293461%2fbest-way-to-update-my-django-model-coming-from-an-external-api-source%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      One simple and common solution is to create a custom Django admin command and use Cron to run it at specified intervals. You can write a command's code to your liking and it can have access to all of the models, settings and other parts of your Django project.



      You would put your code making a request and writing data to the DB, using your Django models, in your new Command class's handle() method (obviously request parameter is no longer needed). And then, if for example you have named your command update_some_data, you can run it as python manage.py update_some_data.



      Assuming Cron exists and is running on the machine. Then you could setup Cron to run this command for you at specified intervals, for example create a file /etc/cron.d/your_app_name and put



      0 4 * * * www-data /usr/local/bin/python /path/to/your/manage.py update_some_data >> /var/log/update_some_data.log  2>&1


      This would make your update be done everyday at 04:00. If your command would provide any output, it will be written to /var/log/update_some_data.log file.
      Of course this is just an example, so your server user running your app (www-data here) and path to the Python executable on the server (/usr/local/bin/python here) should be adjusted for particular use.



      See links for further guidance.






      share|improve this answer




























        2














        One simple and common solution is to create a custom Django admin command and use Cron to run it at specified intervals. You can write a command's code to your liking and it can have access to all of the models, settings and other parts of your Django project.



        You would put your code making a request and writing data to the DB, using your Django models, in your new Command class's handle() method (obviously request parameter is no longer needed). And then, if for example you have named your command update_some_data, you can run it as python manage.py update_some_data.



        Assuming Cron exists and is running on the machine. Then you could setup Cron to run this command for you at specified intervals, for example create a file /etc/cron.d/your_app_name and put



        0 4 * * * www-data /usr/local/bin/python /path/to/your/manage.py update_some_data >> /var/log/update_some_data.log  2>&1


        This would make your update be done everyday at 04:00. If your command would provide any output, it will be written to /var/log/update_some_data.log file.
        Of course this is just an example, so your server user running your app (www-data here) and path to the Python executable on the server (/usr/local/bin/python here) should be adjusted for particular use.



        See links for further guidance.






        share|improve this answer


























          2












          2








          2







          One simple and common solution is to create a custom Django admin command and use Cron to run it at specified intervals. You can write a command's code to your liking and it can have access to all of the models, settings and other parts of your Django project.



          You would put your code making a request and writing data to the DB, using your Django models, in your new Command class's handle() method (obviously request parameter is no longer needed). And then, if for example you have named your command update_some_data, you can run it as python manage.py update_some_data.



          Assuming Cron exists and is running on the machine. Then you could setup Cron to run this command for you at specified intervals, for example create a file /etc/cron.d/your_app_name and put



          0 4 * * * www-data /usr/local/bin/python /path/to/your/manage.py update_some_data >> /var/log/update_some_data.log  2>&1


          This would make your update be done everyday at 04:00. If your command would provide any output, it will be written to /var/log/update_some_data.log file.
          Of course this is just an example, so your server user running your app (www-data here) and path to the Python executable on the server (/usr/local/bin/python here) should be adjusted for particular use.



          See links for further guidance.






          share|improve this answer













          One simple and common solution is to create a custom Django admin command and use Cron to run it at specified intervals. You can write a command's code to your liking and it can have access to all of the models, settings and other parts of your Django project.



          You would put your code making a request and writing data to the DB, using your Django models, in your new Command class's handle() method (obviously request parameter is no longer needed). And then, if for example you have named your command update_some_data, you can run it as python manage.py update_some_data.



          Assuming Cron exists and is running on the machine. Then you could setup Cron to run this command for you at specified intervals, for example create a file /etc/cron.d/your_app_name and put



          0 4 * * * www-data /usr/local/bin/python /path/to/your/manage.py update_some_data >> /var/log/update_some_data.log  2>&1


          This would make your update be done everyday at 04:00. If your command would provide any output, it will be written to /var/log/update_some_data.log file.
          Of course this is just an example, so your server user running your app (www-data here) and path to the Python executable on the server (/usr/local/bin/python here) should be adjusted for particular use.



          See links for further guidance.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 6:36









          NikitaNikita

          3,8741431




          3,8741431

























              1














              For those that are looking for an example:



              from django.core.management.base import BaseCommand
              class Command(BaseCommand):
              def handle(self,*args,**kwargs):
              //Your request api here
              for coin in final_result:
              obj, created = Coincap.objects.update_or_create(
              symbol = coin['symbol'],
              name = coin['name'],
              defaults = {
              'price': coin['priceUsd']})


              Then you run in with cron just as Nikita suggested.






              share|improve this answer




























                1














                For those that are looking for an example:



                from django.core.management.base import BaseCommand
                class Command(BaseCommand):
                def handle(self,*args,**kwargs):
                //Your request api here
                for coin in final_result:
                obj, created = Coincap.objects.update_or_create(
                symbol = coin['symbol'],
                name = coin['name'],
                defaults = {
                'price': coin['priceUsd']})


                Then you run in with cron just as Nikita suggested.






                share|improve this answer


























                  1












                  1








                  1







                  For those that are looking for an example:



                  from django.core.management.base import BaseCommand
                  class Command(BaseCommand):
                  def handle(self,*args,**kwargs):
                  //Your request api here
                  for coin in final_result:
                  obj, created = Coincap.objects.update_or_create(
                  symbol = coin['symbol'],
                  name = coin['name'],
                  defaults = {
                  'price': coin['priceUsd']})


                  Then you run in with cron just as Nikita suggested.






                  share|improve this answer













                  For those that are looking for an example:



                  from django.core.management.base import BaseCommand
                  class Command(BaseCommand):
                  def handle(self,*args,**kwargs):
                  //Your request api here
                  for coin in final_result:
                  obj, created = Coincap.objects.update_or_create(
                  symbol = coin['symbol'],
                  name = coin['name'],
                  defaults = {
                  'price': coin['priceUsd']})


                  Then you run in with cron just as Nikita suggested.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 5:35









                  MachiMachi

                  4117




                  4117






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293461%2fbest-way-to-update-my-django-model-coming-from-an-external-api-source%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Florida Star v. B. J. F.

                      Danny Elfman

                      Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues