Re: Sending json data to postgres once received from API

2020-02-13 Thread Ilya Rustamov
Thank you Jody, I will take a look at this and see how it goes.

On Thursday, February 13, 2020 at 3:27:03 PM UTC-5, Jody Fitzpatrick wrote:
>
> Hello Ilya
>
> I would read the following:
>
> https://docs.djangoproject.com/en/3.0/topics/db/models/
>
> Essentially, what you do in shell is what you would do in your code. 
>
> I also believe you might want to look at how you are grabbing the posted 
> data.
>
> I would use something like url = request.GET.get('url', False)
>
> So you want break your code and can validate if it was indeed sent. 
>
> Best of luck
>
> ps.
>
> Also, look at postgres models.JsonField
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2142e140-794f-4447-ba5e-8f7e69fa11ab%40googlegroups.com.


Re: Sending json data to postgres once received from API

2020-02-13 Thread Jody Fitzpatrick
Hello Ilya

I would read the following:

https://docs.djangoproject.com/en/3.0/topics/db/models/

Essentially, what you do in shell is what you would do in your code. 

I also believe you might want to look at how you are grabbing the posted 
data.

I would use something like url = request.GET.get('url', False)

So you want break your code and can validate if it was indeed sent. 

Best of luck

ps.

Also, look at postgres models.JsonField

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b85cc4ac-54eb-47bb-b78e-2364363f28d6%40googlegroups.com.


Sending json data to postgres once received from API

2020-02-13 Thread Ilya Rustamov
Hey Devs,

   First time poster here, if I am missing anything or am not following 
general rules and practices feel free to let me know.

I am in the process of building an app that asks user for a url and on 
submit makes post request to said url, receives json data and simply prints 
it in a new view.

This part of the app works well and does the job it is supposed to do, my 
only issue is that once the app has received that data, not only do I want 
it printed but would also like for it to be entered into a table in my 
postgres db. 

I have already connected a postgres db and it has a table that is built 
from my models that can receive this data.

I am struggling to understand how to actually send data to the db from the 
app, the only understanding I have of entering data into db is using the 
shell which does not solve my issue. 

My goal is to be able to save the json data i receive from the url into 
that table in my postgres db? What is the best and most efficient way to go 
about accomplishing this?

I will attach some of my code so you can see what I am working with so far.


views.py 

from django.shortcuts import HttpResponse, render
import urllib.request
import json


def home(request):
return render(request, 'djangoAPI/response.html')


def result(request):
url = request.GET['url']
operUrl = urllib.request.urlopen(url)
if operUrl.getcode() == 200:
data = operUrl.read()
jsonData = json.loads(data)
final = json.dumps(jsonData, indent=2, sort_keys=True)
else:
print("Error receiving data", operUrl.getcode())

return render(request, 'djangoAPI/request.html', {'result': final})




models.py

from django.db import models


class URLData(models.Model):
id  = models.IntegerField(primary_key=True)
title   = models.CharField(max_length=200)
summary = models.TextField()
description = models.TextField()
url = models.URLField(max_length=200)
rssFeed = models.TextField(blank=True, null=True)
links   = models.TextField(blank=True, null=True)

def __str__(self):
return self.title



-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/67f33d50-3dca-4391-a54e-ec420d833912%40googlegroups.com.