How to insert JSON result to database?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
Earth rotation discrepancy
How can I watch the 17th (or last, if less) line in files of a folder?
If the first law of thermodynamics ensures conservation of energy, why does it allow systems to lose energy?
How is the list of apps allowed to install another apps populated?
In an emergency, how do I find and share my position?
Is "The life is beautiful" incorrect or just very non-idiomatic?
Why can't an Airbus A330 dump fuel in an emergency?
Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?
Is there any way to keep a player from killing an NPC?
Avoiding racist tropes in fantasy
Is it safe to remove the bottom chords of a series of garage roof trusses?
In the MCU, why does Mjölnir retain its enchantments after Ragnarok?
Was there ever a treaty between 2 entities with significantly different translations to the detriment of one party?
Can you feel passing through the sound barrier in an F-16?
Is my soulless catatonic body a valid target for the Imprisonment spell?
See details of old sessions
How do I request a longer than normal leave of absence period for my wedding?
Notepad++ - How to find multiple values on the same line in any permutation
What is this symbol: semicircles facing eachother
Efficiently pathfinding many flocking enemies around obstacles
How to use "Du hast/ Du hattest'?
Is “I am getting married with my sister” ambiguous?
Why were the crew so desperate to catch Truman and return him to Seahaven?
How should I face my manager if I make a mistake because a senior coworker explained something incorrectly to me?
How to insert JSON result to database?
How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How can I make a time delay in Python?How do I sort a dictionary by value?How to make a chain of function decorators?How to make a flat list out of list of listsHow do I list all files of a directory?How do I POST JSON data with Curl from a terminal/commandline to Test Spring REST?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm building a simple REST using Flask-RESTPlus to get all GitHub user data from their specific username.
I have got the result in JSON format.
But now, I want to insert the result to my database.
I have declared my model columns with same name with key on JSON result.
Here is the snippet of my model:
class User(db.Model):
__tablename__ = "user_v2"
id = db.Column(db.Integer, primary_key=True, index=True)
login = db.Column(db.String, unique=True, index=True)
node_id = db.Column(db.String)
avatar_url = db.Column(db.String)
and let's says the result from API was like this:
"login": "trinanda",
"id": 25479663,
"node_id": "MDQ6VXNlcjI1NDc5NjYz",
"avatar_url": "https://avatars2.githubusercontent.com/u/25479663?v=4"
There are the key on the result was same with my columns name with my models.
I have tried to insert the result with this code, but the things that make me confused are it gives me null value on all of my user columns.
class UserResource(Resource):
def post(self):
schema = UserSchema()
github_field = request.json
username = github_field.get('username')
github_user_data = get_github_user_data(username)
user = User(**github_user_data)
try:
db.session.add(user)
db.session.commit()
except Exception as e:
return str(e)
return
'status': 'success',
'user': schema.dump(user).data
So.. the point of my questions are, how to insert the JSON result to database..?
EDIT:
Here is my github_user_data
:
import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
python rest flask flask-sqlalchemy flask-restplus
|
show 3 more comments
I'm building a simple REST using Flask-RESTPlus to get all GitHub user data from their specific username.
I have got the result in JSON format.
But now, I want to insert the result to my database.
I have declared my model columns with same name with key on JSON result.
Here is the snippet of my model:
class User(db.Model):
__tablename__ = "user_v2"
id = db.Column(db.Integer, primary_key=True, index=True)
login = db.Column(db.String, unique=True, index=True)
node_id = db.Column(db.String)
avatar_url = db.Column(db.String)
and let's says the result from API was like this:
"login": "trinanda",
"id": 25479663,
"node_id": "MDQ6VXNlcjI1NDc5NjYz",
"avatar_url": "https://avatars2.githubusercontent.com/u/25479663?v=4"
There are the key on the result was same with my columns name with my models.
I have tried to insert the result with this code, but the things that make me confused are it gives me null value on all of my user columns.
class UserResource(Resource):
def post(self):
schema = UserSchema()
github_field = request.json
username = github_field.get('username')
github_user_data = get_github_user_data(username)
user = User(**github_user_data)
try:
db.session.add(user)
db.session.commit()
except Exception as e:
return str(e)
return
'status': 'success',
'user': schema.dump(user).data
So.. the point of my questions are, how to insert the JSON result to database..?
EDIT:
Here is my github_user_data
:
import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
python rest flask flask-sqlalchemy flask-restplus
1
Doesgithub_user_data
is being populated? And does it fit exactly on your User model?
– geckos
Mar 27 at 17:34
You should not catchException
directly, instead catch SQLAlchemy base exception
– geckos
Mar 27 at 17:38
Here is my github_user_data:import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
– Harvest 1018
Mar 28 at 0:03
What is the format of github_user_data that you received, please hide the values if they are sensitive data
– geckos
Mar 28 at 0:07
1
flask-sqlalchemy
documentation (at the end of the first section) does not advise against that : "If you decide to override the constructor for any reason, make sure to keep accepting **kwargs and call the super constructor with those **kwargs to preserve this behavior", so you CAN override the constructor and have better control on what it does, or even inspect what's happening in there for debug purposes.
– CloC
Apr 2 at 6:42
|
show 3 more comments
I'm building a simple REST using Flask-RESTPlus to get all GitHub user data from their specific username.
I have got the result in JSON format.
But now, I want to insert the result to my database.
I have declared my model columns with same name with key on JSON result.
Here is the snippet of my model:
class User(db.Model):
__tablename__ = "user_v2"
id = db.Column(db.Integer, primary_key=True, index=True)
login = db.Column(db.String, unique=True, index=True)
node_id = db.Column(db.String)
avatar_url = db.Column(db.String)
and let's says the result from API was like this:
"login": "trinanda",
"id": 25479663,
"node_id": "MDQ6VXNlcjI1NDc5NjYz",
"avatar_url": "https://avatars2.githubusercontent.com/u/25479663?v=4"
There are the key on the result was same with my columns name with my models.
I have tried to insert the result with this code, but the things that make me confused are it gives me null value on all of my user columns.
class UserResource(Resource):
def post(self):
schema = UserSchema()
github_field = request.json
username = github_field.get('username')
github_user_data = get_github_user_data(username)
user = User(**github_user_data)
try:
db.session.add(user)
db.session.commit()
except Exception as e:
return str(e)
return
'status': 'success',
'user': schema.dump(user).data
So.. the point of my questions are, how to insert the JSON result to database..?
EDIT:
Here is my github_user_data
:
import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
python rest flask flask-sqlalchemy flask-restplus
I'm building a simple REST using Flask-RESTPlus to get all GitHub user data from their specific username.
I have got the result in JSON format.
But now, I want to insert the result to my database.
I have declared my model columns with same name with key on JSON result.
Here is the snippet of my model:
class User(db.Model):
__tablename__ = "user_v2"
id = db.Column(db.Integer, primary_key=True, index=True)
login = db.Column(db.String, unique=True, index=True)
node_id = db.Column(db.String)
avatar_url = db.Column(db.String)
and let's says the result from API was like this:
"login": "trinanda",
"id": 25479663,
"node_id": "MDQ6VXNlcjI1NDc5NjYz",
"avatar_url": "https://avatars2.githubusercontent.com/u/25479663?v=4"
There are the key on the result was same with my columns name with my models.
I have tried to insert the result with this code, but the things that make me confused are it gives me null value on all of my user columns.
class UserResource(Resource):
def post(self):
schema = UserSchema()
github_field = request.json
username = github_field.get('username')
github_user_data = get_github_user_data(username)
user = User(**github_user_data)
try:
db.session.add(user)
db.session.commit()
except Exception as e:
return str(e)
return
'status': 'success',
'user': schema.dump(user).data
So.. the point of my questions are, how to insert the JSON result to database..?
EDIT:
Here is my github_user_data
:
import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
python rest flask flask-sqlalchemy flask-restplus
python rest flask flask-sqlalchemy flask-restplus
edited Mar 28 at 0:37
Harvest 1018
asked Mar 27 at 16:46
Harvest 1018Harvest 1018
3671 gold badge2 silver badges19 bronze badges
3671 gold badge2 silver badges19 bronze badges
1
Doesgithub_user_data
is being populated? And does it fit exactly on your User model?
– geckos
Mar 27 at 17:34
You should not catchException
directly, instead catch SQLAlchemy base exception
– geckos
Mar 27 at 17:38
Here is my github_user_data:import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
– Harvest 1018
Mar 28 at 0:03
What is the format of github_user_data that you received, please hide the values if they are sensitive data
– geckos
Mar 28 at 0:07
1
flask-sqlalchemy
documentation (at the end of the first section) does not advise against that : "If you decide to override the constructor for any reason, make sure to keep accepting **kwargs and call the super constructor with those **kwargs to preserve this behavior", so you CAN override the constructor and have better control on what it does, or even inspect what's happening in there for debug purposes.
– CloC
Apr 2 at 6:42
|
show 3 more comments
1
Doesgithub_user_data
is being populated? And does it fit exactly on your User model?
– geckos
Mar 27 at 17:34
You should not catchException
directly, instead catch SQLAlchemy base exception
– geckos
Mar 27 at 17:38
Here is my github_user_data:import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
– Harvest 1018
Mar 28 at 0:03
What is the format of github_user_data that you received, please hide the values if they are sensitive data
– geckos
Mar 28 at 0:07
1
flask-sqlalchemy
documentation (at the end of the first section) does not advise against that : "If you decide to override the constructor for any reason, make sure to keep accepting **kwargs and call the super constructor with those **kwargs to preserve this behavior", so you CAN override the constructor and have better control on what it does, or even inspect what's happening in there for debug purposes.
– CloC
Apr 2 at 6:42
1
1
Does
github_user_data
is being populated? And does it fit exactly on your User model?– geckos
Mar 27 at 17:34
Does
github_user_data
is being populated? And does it fit exactly on your User model?– geckos
Mar 27 at 17:34
You should not catch
Exception
directly, instead catch SQLAlchemy base exception– geckos
Mar 27 at 17:38
You should not catch
Exception
directly, instead catch SQLAlchemy base exception– geckos
Mar 27 at 17:38
Here is my github_user_data:
import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
– Harvest 1018
Mar 28 at 0:03
Here is my github_user_data:
import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
– Harvest 1018
Mar 28 at 0:03
What is the format of github_user_data that you received, please hide the values if they are sensitive data
– geckos
Mar 28 at 0:07
What is the format of github_user_data that you received, please hide the values if they are sensitive data
– geckos
Mar 28 at 0:07
1
1
flask-sqlalchemy
documentation (at the end of the first section) does not advise against that : "If you decide to override the constructor for any reason, make sure to keep accepting **kwargs and call the super constructor with those **kwargs to preserve this behavior", so you CAN override the constructor and have better control on what it does, or even inspect what's happening in there for debug purposes.– CloC
Apr 2 at 6:42
flask-sqlalchemy
documentation (at the end of the first section) does not advise against that : "If you decide to override the constructor for any reason, make sure to keep accepting **kwargs and call the super constructor with those **kwargs to preserve this behavior", so you CAN override the constructor and have better control on what it does, or even inspect what's happening in there for debug purposes.– CloC
Apr 2 at 6:42
|
show 3 more comments
0
active
oldest
votes
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382497%2fhow-to-insert-json-result-to-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382497%2fhow-to-insert-json-result-to-database%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
Does
github_user_data
is being populated? And does it fit exactly on your User model?– geckos
Mar 27 at 17:34
You should not catch
Exception
directly, instead catch SQLAlchemy base exception– geckos
Mar 27 at 17:38
Here is my github_user_data:
import requests
github_scraper_url = 'https://api.github.com/users/'
def get_github_user_data(username):
response = requests.get(github_scraper_url+username)
return response.json()
– Harvest 1018
Mar 28 at 0:03
What is the format of github_user_data that you received, please hide the values if they are sensitive data
– geckos
Mar 28 at 0:07
1
flask-sqlalchemy
documentation (at the end of the first section) does not advise against that : "If you decide to override the constructor for any reason, make sure to keep accepting **kwargs and call the super constructor with those **kwargs to preserve this behavior", so you CAN override the constructor and have better control on what it does, or even inspect what's happening in there for debug purposes.– CloC
Apr 2 at 6:42