Save json data into my db using django and postgresql - json

I'm working with django and postgres, I've a large json data that i want to save into my db but idk how.
My json data looks like this:
{
"Name": "some name",
"Direction": "Street name",
"City": "City",
"Region": "Region",
"DirectionGeo": "Street with geopy",
"Category": "Category",
"ExternalCode": "Code",
"lat": latitude,
"lon": longitude
}
That's all the data I want to save, can someone give me an advice on how I can save my data into the db using python please
this is my model.py:
from django.db import models
class Bidder(models.Model):
name = models.CharField(max_length=500)
direction = models.CharField(max_length=500)
city = models.CharField(max_length=500)
region = models.CharField(max_length=500)
category = models.CharField(max_length=500)
geopy_direction = models.CharField(max_length=500)
lat = models.FloatField(20)
lon = models.FloatField(20)

So if you were trying to consume API data and want to store in your database using python, may this help take reference or in-case i lack to provide correct information, please do let me know
import pymysql
import requests
BASE_URL = 'http://127.0.0.1:8000/'
ENDPOINT = 'api/'
json_data = requests.get(BASE_URL+ENDPOINT)
data = json_data.json()
list_data = []
for edata in data:
list_data.append((edata['EmployeeNo'], edata['Name'], edata['Salary'], edata['Address']))
conn = pymysql.connect(user='root', password='Ashish#123', db='black')
cursor = conn.cursor()
query = 'insert into black_table(EmployeeNo, Name, Salary, Address) values(%s, %s, %s, %s)'
data = list_data
try:
cursor.executemany(query, data)
conn.commit()
print("Data stored to database")
except conn.DatabaseError as message:
if conn:
conn.rollback()
print("Error occured", message)
finally:
if cursor:
cursor.close()
if conn:
conn.close()

Related

Django get primary key with JSON response

How do I send each objects primary key with JSON response?
I am sending json response to amend a table based on users state. Everything works fine, except not being able to get the PK associated with each user.
Currently its sending all values except PK.
class UserByState(View):
page_limit = 100
def get_paginated_context(self, queryset, page, limit):
if not page: page = 1
if limit:
self.page_limit = limit
paginator = Paginator(queryset, self.page_limit)
page_obj = paginator.get_page(page)
serialized_page = serialize("json", page_obj.object_list)
serialized_page = [obj["fields"] for obj in json.loads(serialized_page)]
return {
"data": serialized_page,
"pagination": {
"page": page,
"limit": limit,
"has_next": page_obj.has_next(),
"has_prev": page_obj.has_previous(),
"total": queryset.count()
}
}
def get(self, request, *args, **kwargs):
user = self.request.user
page = request.GET.get('page')
limit = request.GET.get('limit')
state = request.GET.get('state')
queryset = User.objects.all()
if state and state != "all":
queryset = queryset.filter(state=state)
to_return = self.get_paginated_context(queryset, page, limit)
return JsonResponse(to_return, status = 200)
I think this comprehension may lose some values.
serialized_page = serialize("json", page_obj.object_list)
When you call ['fields'] here:
serialized_page = [obj["fields"] for obj in json.loads(serialized_page)]
You get only values of key 'fields'. It's like standard dictionary.
fields = {"first_name": "Dan", "last_name": "ram", "address": "6090 DRAGON WAY", "city": "CREST", "state": "CA", "zipcode": 91912}
Try that:
serialized_page = serialize("json", page_obj.object_list)
serialized_page = [{**obj["fields"], **{"pk": obj["pk"]}} for obj in json.loads(serialized_page)]

Deserializer JSON objects containing JSON Web Signatures in Django. App Store Server Notifications responseBodyV2

I have a python django rest application that I need it to be able to handle post request for App Store Server Notifications.
Thing is that v2 of the App Store Server Notifications payload is in JSON Web Signature (JWS) format, signed by the App Store. Which contains fields that in turn are also in JSON Web Signature (JWS) format, signed by the App Store. I know how to handle that using python-jose procedurally but I can't figure out how to fit the whole process within Django serializers in a graceful manner with as minimal hacking as possible.
The data could be something like:
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJub3RpZmljYXRpb25UeXBlIjoidHlwZSIsInN1YnR5cGUiOiJzdWJfVHlwZSIsIm5vdGlmaWNhdGlvblVVSUQiOiJzdHJpbmcgbm90aWZpY2F0aW9uVVVJRCIsImRhdGEiOnsiYXBwQXBwbGVJZCI6MTIzNCwiYnVuZGxlSWQiOiJhZmRzYXNkIiwiYnVuZGxlVmVyc2lvbiI6ImJ1bmRsZVZlcnNpb24iLCJlbnZpcm9ubWVudCI6ImVudmlyb25tZW50Iiwic2lnbmVkUmVuZXdhbEluZm8iOiJleUpoYkdjaU9pSklVekkxTmlJc0luUjVjQ0k2SWtwWFZDSjkuZXlKaGRYUnZVbVZ1WlhkUWNtOWtkV04wU1dRaU9pSjBaWE4wSUhSdmJHVnRJaXdpWVhWMGIxSmxibVYzVTNSaGRIVnpJam94TENKbGVIQnBjbUYwYVc5dVNXNTBaVzUwSWpvMExDSm5jbUZqWlZCbGNtbHZaRVY0Y0dseVpYTkVZWFJsSWpveE5qTTJOVE0xTVRReExDSnBjMGx1UW1sc2JHbHVaMUpsZEhKNVVHVnlhVzlrSWpwMGNuVmxMQ0p2Wm1abGNrbGtaVzUwYVdacFpYSWlPaUowWlhOMElIUnZiR1Z0SWl3aWIyWm1aWEpVZVhCbElqb3hMQ0p2Y21sbmFXNWhiRlJ5WVc1ellXTjBhVzl1U1dRaU9pSjBaWE4wSUhSdmJHVnRJaXdpY0hKcFkyVkpibU55WldGelpWTjBZWFIxY3lJNk1Td2ljSEp2WkhWamRFbGtJam9pZEdWemRDQjBiMnhsYlNJc0luTnBaMjVsWkVSaGRHVWlPakUyTXpZMU16VXhOREY5LnYwWW9YQUd0MTFPeVBXUk8zV2xTZDRiSWVtcVV6Q0ZJbFdjd0ZwcEI5TmMiLCJzaWduZWRUcmFuc2FjdGlvbkluZm8iOiJleUpoYkdjaU9pSklVekkxTmlJc0luUjVjQ0k2SWtwWFZDSjkuZXlKaGNIQkJZMk52ZFc1MFZHOXJaVzRpT2lKMFpYTjBJSFJ2YkdWdElpd2lZblZ1Wkd4bFNXUWlPaUp6WkdaellYTmtaaUlzSW1WNGNHbHlaWE5FWVhSbElqb3hOak0yTlRNMU1UUXhMQ0pwYmtGd2NFOTNibVZ5YzJocGNGUjVjR1VpT2lKMFpYTjBJSFJ2YkdWdElpd2lhWE5WY0dkeVlXUmxaQ0k2ZEhKMVpTd2liMlptWlhKSlpHVnVkR2xtYVdWeUlqb2lkR1Z6ZENCMGIyeGxiU0lzSW05bVptVnlWSGx3WlNJNk1UUTFMQ0p2Y21sbmFXNWhiRkIxY21Ob1lYTmxSR0YwWlNJNk1UWXpOalV6TlRFME1Td2liM0pwWjJsdVlXeFVjbUZ1YzJGamRHbHZia2xrSWpvaWRHVnpkQ0IwYjJ4bGJTSXNJbkJ5YjJSMVkzUkpaQ0k2SW5SbGMzUWdkRzlzWlcwaUxDSndkWEpqYUdGelpVUmhkR1VpT2pFMk16WTFNelV4TkRFc0luRjFZVzUwYVhSNUlqb3hORFVzSW5KbGRtOWpZWFJwYjI1RVlYUmxJam94TmpNMk5UTTFNVFF4TENKeVpYWnZZMkYwYVc5dVVtVmhjMjl1SWpveE5EVXNJbk5wWjI1bFpFUmhkR1VpT2pFMk16WTFNelV4TkRFc0luTjFZbk5qY21sd2RHbHZia2R5YjNWd1NXUmxiblJwWm1sbGNpSTZJblJsYzNRZ2RHOXNaVzBpTENKMGNtRnVjMkZqZEdsdmJrbGtJam9pZEdWemRDQjBiMnhsYlNJc0luUjVjR1VpT2lKMFpYTjBJSFJ2YkdWdElpd2lkMlZpVDNKa1pYSk1hVzVsU1hSbGJVbGtJam9pZEdWemRDQjBiMnhsYlNKOS5lbnlkTnVwd2txOTNYQ2dfeG5yYzNXTmtNNjM4NXpITnpoa0tqa3cyb3VrIn19.OgSJ4xE3r2Tw0Q4KcwPSD4YFo21uCLDgrKOtKOomijo
and then the part inbetween the dots decoded could look like
b'{"notificationType":"type","subtype":"sub_Type","notificationUUID":"string notificationUUID","data":{"appAppleId":1234,"bundleId":"afdsasd","bundleVersion":"bundleVersion","environment":"environment","signedRenewalInfo":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdXRvUmVuZXdQcm9kdWN0SWQiOiJ0ZXN0IHRvbGVtIiwiYXV0b1JlbmV3U3RhdHVzIjoxLCJleHBpcmF0aW9uSW50ZW50Ijo0LCJncmFjZVBlcmlvZEV4cGlyZXNEYXRlIjoxNjM2NTM1MTQxLCJpc0luQmlsbGluZ1JldHJ5UGVyaW9kIjp0cnVlLCJvZmZlcklkZW50aWZpZXIiOiJ0ZXN0IHRvbGVtIiwib2ZmZXJUeXBlIjoxLCJvcmlnaW5hbFRyYW5zYWN0aW9uSWQiOiJ0ZXN0IHRvbGVtIiwicHJpY2VJbmNyZWFzZVN0YXR1cyI6MSwicHJvZHVjdElkIjoidGVzdCB0b2xlbSIsInNpZ25lZERhdGUiOjE2MzY1MzUxNDF9.v0YoXAGt11OyPWRO3WlSd4bIemqUzCFIlWcwFppB9Nc","signedTransactionInfo":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhcHBBY2NvdW50VG9rZW4iOiJ0ZXN0IHRvbGVtIiwiYnVuZGxlSWQiOiJzZGZzYXNkZiIsImV4cGlyZXNEYXRlIjoxNjM2NTM1MTQxLCJpbkFwcE93bmVyc2hpcFR5cGUiOiJ0ZXN0IHRvbGVtIiwiaXNVcGdyYWRlZCI6dHJ1ZSwib2ZmZXJJZGVudGlmaWVyIjoidGVzdCB0b2xlbSIsIm9mZmVyVHlwZSI6MTQ1LCJvcmlnaW5hbFB1cmNoYXNlRGF0ZSI6MTYzNjUzNTE0MSwib3JpZ2luYWxUcmFuc2FjdGlvbklkIjoidGVzdCB0b2xlbSIsInByb2R1Y3RJZCI6InRlc3QgdG9sZW0iLCJwdXJjaGFzZURhdGUiOjE2MzY1MzUxNDEsInF1YW50aXR5IjoxNDUsInJldm9jYXRpb25EYXRlIjoxNjM2NTM1MTQxLCJyZXZvY2F0aW9uUmVhc29uIjoxNDUsInNpZ25lZERhdGUiOjE2MzY1MzUxNDEsInN1YnNjcmlwdGlvbkdyb3VwSWRlbnRpZmllciI6InRlc3QgdG9sZW0iLCJ0cmFuc2FjdGlvbklkIjoidGVzdCB0b2xlbSIsInR5cGUiOiJ0ZXN0IHRvbGVtIiwid2ViT3JkZXJMaW5lSXRlbUlkIjoidGVzdCB0b2xlbSJ9.enydNupwkq93XCg_xnrc3WNkM6385zHNzhkKjkw2ouk"}}'
and then if the fields encoded in jws format are also decoded the same way mentioned aboce it is going to ultimately look like this:
{
"notificationType":"type",
"subtype":"sub_Type",
"notificationUUID":"string notificationUUID",
"data":
{"appAppleId":1234,
"bundleId":"afdsasd",
"bundleVersion":"bundleVersion",
"environment":"environment",
"signedRenewalInfo":{
"autoRenewProductId": "test tolem",
"autoRenewStatus": 1,
"expirationIntent" : 4,
"gracePeriodExpiresDate": 1636535141,
"isInBillingRetryPeriod": true,
"offerIdentifier": "test tolem",
"offerType": 1,
"originalTransactionId": "test tolem",
"priceIncreaseStatus": 1,
"productId": "test tolem",
"signedDate": 1636535141,
},
"signedTransactionInfo":{
"appAccountToken": "test tolem",
"bundleId": "sdfsasdf",
"expiresDate" : 1636535141,
"inAppOwnershipType": "test tolem",
"isUpgraded": true,
"offerIdentifier": "test tolem",
"offerType": 145,
"originalPurchaseDate": 1636535141,
"originalTransactionId": "test tolem",
"productId": "test tolem",
"purchaseDate": 1636535141,
"quantity": 145,
"revocationDate": 1636535141,
"revocationReason": 145,
"signedDate": 1636535141,
"subscriptionGroupIdentifier": "test tolem",
"transactionId": "test tolem",
"type": "test tolem",
"webOrderLineItemId": "test tolem"
}}}
Which is what I want to store into my database tables
Any help or idea is going to be greatly appriciated
For anybody possibly dealing with the same issues
this is how I handled the serialization it looked like it's working fine
from .models import TransactionV2, RenewalInfoV2,
AppStoreDecodedPayloadV2, AppStoreDataV2
from rest_framework import serializers
from jose import jwt
import base64
import io
from rest_framework.parsers import JSONParser
import json
class PayloadField(serializers.Field):
def to_internal_value(self, obj):
content = obj.split('.')[1]
jws_payload = base64.b64decode(content)
data = json.loads(jws_payload.decode())
serializer = AppStoreDecodedPayloadSerializerV2(data = data)
if serializer.is_valid():
return serializer.validated_data
class SignedTransactionInfo(serializers.Field):
def to_internal_value(self, obj):
content = obj.split('.')[1]
jws_payload = base64.b64decode(content)
data = json.loads(jws_payload.decode())
serializer = TransactionV2Serializer(data = data)
if serializer.is_valid():
return serializer.validated_data
class SignedRenewalInfoField(serializers.Field):
def to_internal_value(self, obj):
content = obj.split('.')[1]
algorithm = obj.split('.')[0]
secret = obj.split('.')[2]
jws_payload = base64.b64decode(content)
jws_payload_algo = base64.b64decode(algorithm)
algo = json.loads(jws_payload_algo.decode())
data = json.loads(jws_payload.decode())
serializer = RenewSerializer(data = data)
if serializer.is_valid():
return serializer.validated_data
class AppStoreNotificationSerializerReuturnV2(serializers.Serializer):
signedPayload = PayloadField()
def create(self, validated_data):
app_store_decoded_payload_data = validated_data.pop('signedPayload')
app_store_sub = AppStoreDecodedPayloadSerializerV2.create(app_store_decoded_payload_data)
return app_store_sub
class AppStoreDecodedPayloadDataSerializer(serializers.Serializer):
appAppleId = serializers.IntegerField()
bundleId = serializers.CharField()
bundleVersion = serializers.CharField()
environment = serializers.CharField()
signedRenewalInfo = SignedRenewalInfoField()
signedTransactionInfo = SignedTransactionInfo()
def create(**decoded_payload_data):
renewall_data = decoded_payload_data.pop('signedRenewalInfo')
transaction_data = decoded_payload_data.pop('signedTransactionInfo')
app_store_data = AppStoreDataV2.objects.create(**decoded_payload_data)
TransactionV2.objects.create(**transaction_data ,app_store_data = app_store_data)
RenewalInfoV2.objects.create(**renewall_data, app_store_data = app_store_data)
return app_store_data
class AppStoreDecodedPayloadSerializerV2(serializers.Serializer):
notificationType = serializers.CharField()
subtype = serializers.CharField()
notificationUUID = serializers.CharField()
data = AppStoreDecodedPayloadDataSerializer()
def create(app_store_decoded_payload):
app_store_decoded_payload_data = app_store_decoded_payload.pop('data')
decoded_payload = AppStoreDecodedPayloadV2.objects.create(**app_store_decoded_payload)
app_store_decoded_payload =AppStoreDecodedPayloadDataSerializer.create(**app_store_decoded_payload_data, app_store_decoded_payload = decoded_payload)
return app_store_decoded_payload
class TransactionV2Serializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = TransactionV2
class RenewSerializer(serializers.ModelSerializer):
class Meta:
fields = '__all__'
model = RenewalInfoV2

How can you add nested arrays and objects within json responses using flask and jsonify

I'm currently following this example to get an API up and running (my code below)
from flask import Flask, request, jsonify
from flask_restful import Resource, Api
from sqlalchemy import create_engine
from json import dumps
import mysql.connector
db_connect = create_engine('mysql+pymysql://root:pass#localhost/news')
app = Flask(__name__)
api = Api(app)
class Events(Resource):
def get(self):
conn = db_connect.connect() # connect to database
query = conn.execute("select id, title, description, date, scheduled,favourite,count_audio,count_video,count_articles from events;")
result = [dict(zip(tuple (query.keys()) ,i)) for i in query.cursor]
return jsonify(result)
api.add_resource(Events, '/events') # Route_1
if __name__ == '__main__':
app.run(port='5002')
And I want to end up with a nested array, tags, and a nested object, location, as below
[{
"description": "TEST DESCRIPTION",
"id": 1,
"title": "TEST TITLE",
"date": "2020-09-02",
"scheduled":"true",
"favourite":"true",
"tags": ["celebration", "national holiday"],
"location": {
"state": {
"name": "new zealand",
"affiliation": ["United Nations"]
},
"province": "",
"urbanisation": "Wellington"
}
},
{
"description": "LONG DESCRIPTION",
"id": 2,
"title": "SECOND ENTRY",
"date": "2020-09-03",
"scheduled":"false",
"favourite":"false",
"tags": ["election", "national holiday"],
"location": {
"state": {
"name": "Brazil",
"affiliation": [""]
},
"province": "",
"urbanisation": ""
}
}]
Is something like this even possible? Do I need to rethink my API to have endpoints covering each nested object?
api.add_resource(Events, '/events')
api.add_resource(tags, '/events/tag')
api.add_resource(location, '/events/location')
Or, do I nest dictionaries as in this answer.
Yes its possible and totally valid. But I wouldn't query the database directly. I would use an ORM like SQLAlchemy, for flask use can use flask-sqlalchemy extension. The serialization can be achieved using flask-marshmallow
Use SQLAlchemy to create your models:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'
db = SQLAlchemy(app)
class EventModel(db.Model):
__tablename__ = "event"
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String, nullable=True)
class LocationModel(db.Model):
__tablename__ = "location"
id = db.Column(db.Integer, primary_key=True)
province = db.Column(db.String, nullable=True)
event_id = db.Column(db.Integer, db.ForeignKey("event.id", ondelete="CASCADE"), nullable=False)
event = db.relationship("EventModel", backref=db.backref("locations", lazy=True))
Then you need serializer classes (employing flask-marshmallow) to handle the serialization:
from marshmallow_sqlalchemy import SQLAlchemyAutoSchema, fields
from marshmallow_sqlalchemy import auto_field
class LocationSchema(SQLAlchemyAutoSchema):
class Meta:
model = LocationModel
load_instance = True
include_fk = False
id = auto_field(load_only=True)
class EventSchema(SQLAlchemyAutoSchema):
class Meta:
model = EventModel
load_instance = True
include_fk = False
id = auto_field(load_only=True)
locations = fields.Nested(LocationSchema, many=True)
And finally, you can serialize to json as follows:
db.drop_all()
db.create_all()
event = EventModel(title="Summer Show")
db.session.add(event)
db.session.commit()
location1 = LocationModel(province="Upper Austria", event_id=event.id)
db.session.add(location1)
location2 = LocationModel(province="Tirol", event_id=event.id)
db.session.add(location2)
db.session.commit()
schema = EventSchema()
result = db.session.query(EventModel).all()
print(schema.dump(result, many=True))
A complete tutorial can be found here.

Update TFS Web with a Python script

How can I update a certain field in tfs web with Python?
I have connected to tfs and have received an HTML response.
I have a json with the data that I would like to insert to tfs testCases field.
JSON:
data = json.loads(url.read().decode()) (Json external data)
HTML:
tfsResponse = requests.get(tfsApi, auth=HttpNtlmAuth(username, password))
if tfsResponse.ok:
print(tfsResponse)
soup = BeautifulSoup(tfsResponse.text, 'lxml')
How can I do it?
It's not able to directly use Jenkins automation tests results to update TFS test case.
You need use Rest API to handle this. You need to extract the test results fist then update them to TFS server.
With using below Rest API:
PATCH https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/results?api-version=5.1
Sample body
[
{
"state": "Completed",
"testPoint": {
"id": 10
},
"outcome": "Passed",
"testCase": {
"id": 4567
}
}
]
If you want to use code, a code snippet for your reference, should similar to Python:
try
{
var u = new Uri("https://{My Account}.visualstudio.com");
VssCredentials c = new VssCredentials(new Microsoft.VisualStudio.Services.Common.VssBasicCredential(string.Empty, "PAT"));
var connection = new VssConnection(u, c);
var testClient = connection.GetClient<TestManagementHttpClient>();
int testpointid = 1;
string teamProject = "MyProjectName";
RunCreateModel run = new RunCreateModel(name: "TestCase Name", plan: new Microsoft.TeamFoundation.TestManagement.WebApi.ShallowReference("TestPlan Id"), pointIds: new int[] { testpointid });
TestRun testrun = testClient.CreateTestRunAsync(run, teamProject).Result;
TestCaseResult caseResult = new TestCaseResult() { State = "Completed", Outcome = "passed", Id = 100000 };
var testResults = testClient.UpdateTestResultsAsync(new TestCaseResult[] { caseResult }, teamProject, testrun.Id).Result;
RunUpdateModel runmodel = new RunUpdateModel(state: "Completed");
TestRun testRunResult = testClient.UpdateTestRunAsync(runmodel, teamProject, testrun.Id, runmodel).Result;
}
catch (AggregateException e)
{
Console.WriteLine(e.InnerException.Message);
}

Serializing data results from query with select_related into json

how do i serialize output of a query with select related into a json ?
When i serialize data from query with select_related, the resultant json doesn't contain related field data.
Table schema.
from django.db import models
class User(models.Model)
username = models.CharField(max_length=30)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=50)
class Userinfo(models.Model)
userId = models.oneToOneField(User)
userImg = models.TextField()
city = models.CharField(max_length=30)
says = models.CharField(max_length=200)
Query using select related
from django.core import serializers
json_serializer = serializers.get_serialier("json")()
uinfo = Userinfo.objects.filter(userId=1).select_related('userId')
response = json_serializer.serialize(uinfo, ensure_ascii=False, indent=2, use_natural_keys=True)
The result of the response is
[{
"pk": 1,
"model": "userinfo",
"fields": {
"city": "mycity",
"says": "Hi, its me",
"userImg": "profile_img.png",
"userId": [ "user1" ]
}
}]
while the result suppose to be
[{
"pk": 1,
"model": "userinfo",
"fields": {
"city": "mycity",
"says": "Hi, its me",
"userImg": "profile_img.png",
"userId": [{
"username" : "user1",
"first_name" : "User",
"last_name" : "ls1",
"email" : "user1#host.com"
}]
}
}]
I changed the query using values instead of select related but i go this error
uinfo = Userinfo.objects.filter(userId=1).values('userImg', 'city', 'says', 'userId__username', 'userId__first_name', 'userId__email')
response = json_serializer.serialize(uinfo, ensure_ascii=False, indent=2, use_natural_keys=True)
Error:
concrete_model = obj._meta.concrete_model
AttributeError: 'dict' object has no attribute '_meta'
I have tried some solutions found on stackoverflow too but i got errors during serializing
First method
from rest_framework.renderers import JSONRenderer
uinfo = Userinfo.objects.filter(userId=1).select_related('userId')
response = JSONRenderer().render(uinfo)
and the type() of uinfo is
<class 'django.db.models.query.QuerySet'>
Error:
TypeError: [<Userinfo: Userinfo object>] is not JSON serializable
Second method:
from rest_framework.renderers import JSONRenderer
uinfo = Userinfo.objects.filter(userId=1).values('userImg', 'city', 'says', 'userId__username', 'userId__first_name')
response = JSONRenderer().render(uinfo)
and the type() of uinfo returned is class
<'django.db.models.query.ValuesQuerySet>
Error:
TypeError: [{'userImg': u'profile_img.png', 'city': u'mycity', 'says' : u'Hi, its me' 'userId__username': u'user1', 'userId__first_name': u'user'}] is not JSON serializable
I also tried to convert query output result into a dict(using https://djangosnippets.org/snippets/2670/ ) before serializing into json but i got the following error
uinfo = Userinfo.objects.filter(userId=1).select_related('userId')
uinfodata = deep_dump_instance(uinfo)
Error:
in deep_dump_instance
if (instance.__class__, instance.pk) in seen:
AttributeError: 'QuerySet' object has no attribute 'pk'
views.py
from django.utils import simplejson
def convert_to_json():
uinfo = Userinfo.objects.filter(userId_id__in=inner_qs).values('userId__username', 'userImg', 'says', 'userId__first_name', 'city')
lusers = ValuesQuerySetToDict(users)
response = simplejson.dumps(lusers)
return response
def ValuesQuerySetToDict(vqs):
return [item for item in vqs]