Storing lists from api into database

2018-04-06 Thread Mukesh Jha
My main project is to collect and show the results from news site of a 
particular topic. I have use the guardian's api and I have been given 4 
keywords. If I click on any one of the keyword, I need to display all the 
recent articles related to it. Now what I have understood is:

import json,urllib.request
data = json.load(urllib.request.urlopen('
https://content.guardianapis.com/search?q=gst&api-key=test'))
lurl=[]  #list to store the url of articles
ldate=[]   #list to store the date of publish of url
for i in range(0,10):
  lurl.append(data['response']['results'][i]['webUrl'])
  ldate.append(data['response']['results'][i]['webPublicationDate'])
for i in range(0,10):
  print(ldate[i]+" : ",end="")
  print(lurl[i])

Now I have to store all such values in my database and call it in views 
page.

class tesla(models.Model):
id1=models.CharField(max_length=1)
webTitle=models.CharField(max_length=1)
webUrl=models.CharField(max_length=1)
apiUrl=models.CharField(max_length=1)
webPublicationDat=models.CharField(max_length=1)

How can I do it? Please help.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/84a010c6-2d11-49d9-956a-0a547870d6df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Storing lists from api into database

2018-04-06 Thread Mateusz Kurowski
Maybe you should read the documentation first ? :))

2018-04-06 22:03 GMT+02:00 Mukesh Jha :

> My main project is to collect and show the results from news site of a
> particular topic. I have use the guardian's api and I have been given 4
> keywords. If I click on any one of the keyword, I need to display all the
> recent articles related to it. Now what I have understood is:
>
> import json,urllib.request
> data = json.load(urllib.request.urlopen('https://content.
> guardianapis.com/search?q=gst&api-key=test'))
> lurl=[]  #list to store the url of articles
> ldate=[]   #list to store the date of publish of url
> for i in range(0,10):
>   lurl.append(data['response']['results'][i]['webUrl'])
>   ldate.append(data['response']['results'][i]['webPublicationDate'])
> for i in range(0,10):
>   print(ldate[i]+" : ",end="")
>   print(lurl[i])
>
> Now I have to store all such values in my database and call it in views
> page.
>
> class tesla(models.Model):
> id1=models.CharField(max_length=1)
> webTitle=models.CharField(max_length=1)
> webUrl=models.CharField(max_length=1)
> apiUrl=models.CharField(max_length=1)
> webPublicationDat=models.CharField(max_length=1)
>
> How can I do it? Please help.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/84a010c6-2d11-49d9-956a-0a547870d6df%40googlegroups.com
> 
> .
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAO_JuYN3PyMm8%3D1kPHT28WOrqJ4nSMhgY120kAQ6tjTaW%3Dj4BA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: Storing lists from api into database

2018-04-06 Thread James Farris
I would use the Requests module.  It's much nicer and comes loaded with 
stuff.  It's a module used to connect to API's.  Here's the docs: 
http://docs.python-requests.org/en/master/

As far as using what you have, to save it to the database is just another 
few lines of code.
You have to import the database model

from YOUR_APP import tesla

Then in loop in your view just add the following to save the data to the 
database:

tesla = tesla()
tesla.webUrl = data['response']['results'][i]['webUrl]
tesla.webPublicationDat = 
data['response']['results'][i]['webPublicationDate]
tesla.save()

A few things.  It's best practice to Capitalize your models( i.e. class 
Tesla, not tesla) 
If your intention is to generate an id in the id1 field, there is no need 
to have it because Django gives you an id field automatically.


On Friday, April 6, 2018 at 8:03:10 PM UTC, Mukesh Jha wrote:
>
> My main project is to collect and show the results from news site of a 
> particular topic. I have use the guardian's api and I have been given 4 
> keywords. If I click on any one of the keyword, I need to display all the 
> recent articles related to it. Now what I have understood is:
>
> import json,urllib.request
> data = json.load(urllib.request.urlopen('
> https://content.guardianapis.com/search?q=gst&api-key=test'))
> lurl=[]  #list to store the url of articles
> ldate=[]   #list to store the date of publish of url
> for i in range(0,10):
>   lurl.append(data['response']['results'][i]['webUrl'])
>   ldate.append(data['response']['results'][i]['webPublicationDate'])
> for i in range(0,10):
>   print(ldate[i]+" : ",end="")
>   print(lurl[i])
>
> Now I have to store all such values in my database and call it in views 
> page.
>
> class tesla(models.Model):
> id1=models.CharField(max_length=1)
> webTitle=models.CharField(max_length=1)
> webUrl=models.CharField(max_length=1)
> apiUrl=models.CharField(max_length=1)
> webPublicationDat=models.CharField(max_length=1)
>
> How can I do it? Please help.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/190437bf-a306-446f-96ec-6537f0001475%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Storing lists from api into database

2018-04-06 Thread James Farris
I would use the Requests module.  It's much nicer and comes loaded with 
stuff.  It's a module used to connect to API's.  Here's the docs: 
http://docs.python-requests.org/en/master/

As far as using what you have, to save it to the database is just another 
few lines of code.
You have to import the database model

from YOUR_APP.models import tesla

Then in loop in your view just add the following to save the data to the 
database:

tesla = tesla()
tesla.webUrl = data['response']['results'][i]['webUrl]
tesla.webPublicationDat = 
data['response']['results'][i]['webPublicationDate]
tesla.save()

A few things.  It's best practice to Capitalize your models( i.e. class 
Tesla, not tesla) 
If your intention is to generate an id in the id1 field, there is no need 
to have it because Django gives you an id field automatically.

On Friday, April 6, 2018 at 8:03:10 PM UTC, Mukesh Jha wrote:
>
> My main project is to collect and show the results from news site of a 
> particular topic. I have use the guardian's api and I have been given 4 
> keywords. If I click on any one of the keyword, I need to display all the 
> recent articles related to it. Now what I have understood is:
>
> import json,urllib.request
> data = json.load(urllib.request.urlopen('
> https://content.guardianapis.com/search?q=gst&api-key=test'))
> lurl=[]  #list to store the url of articles
> ldate=[]   #list to store the date of publish of url
> for i in range(0,10):
>   lurl.append(data['response']['results'][i]['webUrl'])
>   ldate.append(data['response']['results'][i]['webPublicationDate'])
> for i in range(0,10):
>   print(ldate[i]+" : ",end="")
>   print(lurl[i])
>
> Now I have to store all such values in my database and call it in views 
> page.
>
> class tesla(models.Model):
> id1=models.CharField(max_length=1)
> webTitle=models.CharField(max_length=1)
> webUrl=models.CharField(max_length=1)
> apiUrl=models.CharField(max_length=1)
> webPublicationDat=models.CharField(max_length=1)
>
> How can I do it? Please help.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/eb4b2947-7730-436f-9dae-3eafeb66e287%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.