Place Primary key inside JSON response - python

The JSON response i'm getting is as below.
In my code i'am trying to fetch the list from the db, as per above img. In the result primary key is coming outside of the fields for each record. How can i place it inside the fields for every record like.
"results":[
"fields":{
"pk": "F09087687633",
"company_name": "Tata",
}
]
Below is my code:
views.py (In below code for loop is to remove app name from the results, same i use to remove primary key, it is working but how can i place it inside for each fields.)
#csrf_exempt
def fleet_owner_list(request):
page_number = json.loads(request.body)
records,total_pages = FleetOwner.get_all_owners(page_number)
for data in range(len(records)):
del records[data]['model']
returnObject = {
"page" : page_number,
"total_results":len(records),
"total_pages":total_pages,
"status" : messages.RETRIVAL_SUCCESS,
"message" : messages.FLEETOWNER_DATA_RETRIEVE_SUCCESS,
"results" : records
}
models.py
#classmethod
def get_all_owners(cls,page_number):
data = cls.objects.filter(is_deleted = False)
page_numbers = page_number
pegination_result, total_page_count = list_paginate(data, page_numbers)
data = serializers.serialize("json", pegination_result)
data = json.loads(data)
return data, total_page_count
paginator.py (This is a common function i'm using for all the list functions to perform django pagination.)
def list_paginate(data,page_numbers):
paginator = Paginator(data,10)
page = page_numbers
try :
records = paginator.page(page)
except PageNotAnInteger:
records = paginator.page(1)
except EmptyPage:
records = paginator.page(paginator.num_pages)
return records, paginator.num_pages
pegination_result, total_page_count = list_paginate(data, page_numbers)
data = serializers.serialize("json", pegination_result)
data = json.loads(data)
return data, total_page_count

By default this is how Django serialize a queryset into JSON Objects. The Django documentation also state the same. The whole collection is just represented as an array and the objects are represented by JSON objects with three properties: “pk”, “model” and “fields”. “fields” is again an object containing each field’s name and value as property and property-value respectively. i.e.,
[
{
"pk": "4b678b301dfd8a4e0dad910de3ae245b",
"model": "sessions.session",
"fields": {
"expire_date": "2013-01-16T08:16:59.844Z",
...
}
}
]
If you look at the Serializer class implementation you can find a get_dump_object method which is responsible for the final JSON output for an object. i.e.,
def get_dump_object(self, obj):
data = {'model': str(obj._meta)}
if not self.use_natural_primary_keys or not hasattr(obj, 'natural_key'):
data["pk"] = self._value_from_field(obj, obj._meta.pk)
data['fields'] = self._current
return data
In other teams this is the method responsible for the below format,
{
"pk": "pk",
"model": "model",
"fields": {
"field1": "2013-01-16T08:16:59.844Z",
...
}
}
Since you want the pk field inside the fields you should create your own JSON serializer class and override the get_dump_object method like the below one,
>>> import json
>>> from pprint import pprint
>>> from django.core.serializers.json import Serializer as JSONSerializer
>>>
>>> class MyCustomSerializer(JSONSerializer):
... def get_dump_object(self, obj):
... data = {'model': str(obj._meta)}
... data['fields'] = self._current
... if not self.use_natural_primary_keys or not hasattr(obj, 'natural_key'):
... data['fields'].update({'pk': self._value_from_field(obj, obj._meta.pk)})
... return data
...
>>> pprint(json.loads(MyCustomSerializer().serialize(User.objects.all())))
[{'fields': {'date_joined': '2019-07-13T05:52:37.885Z',
'email': 'user1#gmail.com',
'first_name': 'User',
'groups': [],
'is_active': True,
'is_staff': False,
'is_superuser': False,
'last_login': None,
'last_name': '1',
'password': '',
'pk': 1,
'user_permissions': [],
'username': ''},
'model': 'auth.user'}]
# You can see that `pk` is now inside the 'fields' key.
>>> json.loads(MyCustomSerializer().serialize(User.objects.all()))[0]['fields']['pk']
1

Related

flask-restplus fields.Nested() with raw Dict (not model)

Spoiler alert: I posted my solution as an answer to this question
I am using flastk-resptlus to create an API. I have to provide the data in a specific structure, which I have problems to get, see an example below:
What I need to get is this structure:
{
"metadata": {
"files": []
},
"result" : {
"data": [
{
"user_id": 1,
"user_name": "user_1",
"user_role": "editor"
},
{
"user_id": 2
"user_name": "user_2",
"user_role": "editor"
},
{
"user_id": 3,
"user_name": "user_3",
"user_role": "curator"
}
]
}
}
But the problem comes that I cannot manage to get the structure of "result" : { "data": []} without making "data" a model itself.
What I tried to do so far (and did not work)
# define metadata model
metadata_model = api.model('MetadataModel', {
"files": fields.List(fields.String(required=False, description='')),
}
# define user model
user_model = api.model('UserModel', {
"user_id": fields.Integer(required=True, description=''),
"user_name": fields.String(required=True, description=''),
"user_role": fields.String(required=False, description='')
}
# here is where I have the problems
user_list_response = api.model('ListUserResponse', {
'metadata': fields.Nested(metadata_model),
'result' : {"data" : fields.List(fields.Nested(user_model))}
})
Complains that cannot get the "schema" from "data" (because is not a defined model), but I don't want to be a new api model, just want to append a key called "data". Any suggestions?
This I tried and works, but is not what I want (because I miss the "data"):
user_list_response = api.model('ListUserResponse', {
'metadata': fields.Nested(metadata_model),
'result' : fields.List(fields.Nested(user_model))
})
I don't want data to be a model because the common structure of the api is the following:
{
"metadata": {
"files": []
},
"result" : {
"data": [
<list of objects> # here must be listed the single model
]
}
}
Then, <list of objects> can be users, addresses, jobs, whatever.. so I want to make a "general structure" in which then I can just inject the particular models (UserModel, AddressModel, JobModel, etc) without creating a special data model for each one.
A possible approach is to use fields.Raw which returns whatever serializable object you pass. Then, you can define a second function, which creates your result and uses marshal. marshal transforms your data according to a model and accepts an additional parameter called envelope. envelope surrounds your modeled data by a given key and does the trick.
from flask import Flask
from flask_restplus import Api, fields, Resource, marshal
app = Flask(__name__)
api = Api()
api.init_app(app)
metadata_model = api.model("metadata", {
'file': fields.String()
})
user_model = api.model('UserModel', {
"user_id": fields.Integer(required=True, description=''),
"user_name": fields.String(required=True, description=''),
"user_role": fields.String(required=False, description='')
})
response_model = api.model("Result", {
'metadata': fields.List(fields.Nested(metadata_model)),
'result': fields.Raw()
})
#api.route("/test")
class ApiView(Resource):
#api.marshal_with(response_model)
def get(self):
data = {'metadata': {},
'result': self.get_user()}
return data
def get_user(self):
# Access database and get data
user_data = [{'user_id': 1, 'user_name': 'John', 'user_role': 'editor'},
{'user_id': 2, 'user_name': 'Sue', 'user_role': 'curator'}]
# The kwarg envelope does the trick
return marshal(user_data, user_model, envelope='data')
app.run(host='0.0.0.0', debug=True)
My workaround solution that solves all my problems:
I create a new List fields class (it is mainly copied from fields.List), and then I just tune the output format and the schema in order to get the 'data' as key:
class ListData(fields.Raw):
'''
Field for marshalling lists of other fields.
See :ref:`list-field` for more information.
:param cls_or_instance: The field type the list will contain.
This is a modified version of fields.List Class in order to get 'data' as key envelope
'''
def __init__(self, cls_or_instance, **kwargs):
self.min_items = kwargs.pop('min_items', None)
self.max_items = kwargs.pop('max_items', None)
self.unique = kwargs.pop('unique', None)
super(ListData, self).__init__(**kwargs)
error_msg = 'The type of the list elements must be a subclass of fields.Raw'
if isinstance(cls_or_instance, type):
if not issubclass(cls_or_instance, fields.Raw):
raise MarshallingError(error_msg)
self.container = cls_or_instance()
else:
if not isinstance(cls_or_instance, fields.Raw):
raise MarshallingError(error_msg)
self.container = cls_or_instance
def format(self, value):
if isinstance(value, set):
value = list(value)
is_nested = isinstance(self.container, fields.Nested) or type(self.container) is fields.Raw
def is_attr(val):
return self.container.attribute and hasattr(val, self.container.attribute)
# Put 'data' as key before the list, and return the dict
return {'data': [
self.container.output(idx,
val if (isinstance(val, dict) or is_attr(val)) and not is_nested else value)
for idx, val in enumerate(value)
]}
def output(self, key, data, ordered=False, **kwargs):
value = fields.get_value(key if self.attribute is None else self.attribute, data)
if fields.is_indexable_but_not_string(value) and not isinstance(value, dict):
return self.format(value)
if value is None:
return self._v('default')
return [marshal(value, self.container.nested)]
def schema(self):
schema = super(ListData, self).schema()
schema.update(minItems=self._v('min_items'),
maxItems=self._v('max_items'),
uniqueItems=self._v('unique'))
# work around to get the documentation as I want
schema['type'] = 'object'
schema['properties'] = {}
schema['properties']['data'] = {}
schema['properties']['data']['type'] = 'array'
schema['properties']['data']['items'] = self.container.__schema__
return schema

Pass Django queryset to Highcharts.js ---TypeError: values() takes no arguments (1 given)

i'm trying to pass my queryset to highcharts.js. I am able to do it using the below method:
This is my view.py
class chart_data(object): # Grabs the Data
def sum_savings():
data = {'division': [], 'total_actual_savings': [],
'actual_fte': []}
cost = BsoCet.objects.annotate(savings=Sum('total_actual_savings'))
for i in cost:
data['division'].append(i.division)
data['total_actual_savings'].append(i.total_actual_savings)
#data['actual_fte'].append(i.actual_fte)
return data
def plot(request, chartID = 'chart_ID', chart_type = 'column',chart_height
= 500):
data = chart_data.sum_savings()
chart = {
'chart' : {"renderTo": chartID, "type": chart_type, "height":
chart_height},
'title' : {"text": 'Check Cost per Division'},
'xAxis' : {"title": {"text": 'Divisions'}, "categories":
data['division']},
'yAxis' : {"title": {"text": 'Cost'}},
'series' : [
{"name": 'Total Actual Savings', "data":
data['total_actual_savings']}
]
}
return JsonResponse(chart)
This is my template
<div id="container" data-url="{% url 'BSO:plot' %}"></div>
<script>
$.ajax({
url: $("#container").attr("data-url"),
dataType: 'json',
success: function (data) {
Highcharts.chart("container", data);
}
});
</script>
When I start using values() in my queryset to annotate and group columns for aggregation and append them to the data variable, it gives me this error TypeError: values() takes no arguments (1 given).
This is my new views.py using values() for my queryset:
class chart_data(object): # Grabs the Data
def sum_savings():
data = {'division': [], 'total_actual_savings': [],
'actual_fte': []}
cost = BsoCet.objects.values('division').
annotate(savings=Sum('total_actual_savings'))
for i in cost:
data['division'].append(i.values('division'))
data['total_actual_savings'].
append(i.values('total_actual_savings'))
return data
**** def plot remains the same...
*** this is the error I get:
line 21, in sum_savings
data['division'].append(i.values('division'))
TypeError: values() takes no arguments (1 given)
I am fairly new to Django, so please help me.
Found a solution over the internet. Changed my views.py into this:
def Chart(request):
dataset = BsoCet.objects.values('division').
annotate(savings=Sum('total_actual_savings')).order_by('savings')
categories = list()
savings_data = list()
for entry in dataset:
categories.append(entry['division'])
savings_data.append(entry['savings'])
savings = {
'name': 'savings',
'data': savings_data,
'color': 'green'
}
chart = {
'chart': {'type': 'column'},
'title': {'text': 'Division Savings'},
'xAxis': {'categories': categories},
'series': [savings]
}
dump = json.dumps(chart)
return render(request, 'BSO/BSO_dashboard.html', {'chart':dump})

Google DLP: "ValueError: Protocol message Value has no "stringValue" field."

I have a method where I build a table for multiple items for Google's DLP inspect API which can take either a ContentItem, or a table of values
Here is how the request is constructed:
def redact_text(text_list):
dlp = google.cloud.dlp.DlpServiceClient()
project = 'my-project'
parent = dlp.project_path(project)
items = build_item_table(text_list)
info_types = [{'name': 'EMAIL_ADDRESS'}, {'name': 'PHONE_NUMBER'}]
inspect_config = {
'min_likelihood': "LIKELIHOOD_UNSPECIFIED",
'include_quote': True,
'info_types': info_types
}
response = dlp.inspect_content(parent, inspect_config, items)
return response
def build_item_table(text_list):
rows = []
for item in text_list:
row = {"values": [{"stringValue": item}]}
rows.append(row)
table = {"table": {"headers": [{"name": "something"}], "rows": rows}}
return table
When I run this I get back the error ValueError: Protocol message Value has no "stringValue" field. Even though the this example and the docs say otherwise.
Is there something off in how I build the request?
Edit: Here's the output from build_item_table
{
'table':
{
'headers':
[
{'name': 'value'}
],
'rows':
[
{
'values':
[
{
'stringValue': 'My name is Jenny and my number is (555) 867-5309, you can also email me at anemail#gmail.com, another email you can reach me at is email#email.com. '
}
]
},
{
'values':
[
{
'stringValue': 'Jimbob Doe (555) 111-1233, that one place down the road some_email#yahoo.com'
}
]
}
]
}
}
Try string_value .... python uses the field names, not the type name.

Django Rest fields grouping and customising

I have a Django models like so:
class Floor(models.Model):
name = models.CharField(max_lenght =100)
class Point(models.Model):
created_at = fields.UnixDateTimeField(auto_now_add=True)
updated_at = fields.UnixDateTimeField(auto_now=True)
floor = models.ForeignKey(Floor)
device = models.CharField(max_lenght=100)
creates_at and updated_at are just custom fields with timestamps.
So, i need to send request like points/?start=x,end=y,timeslice=z, where x -
is start timestamp, y - end timestamp, z - timeslice in this period. For example, if x is start of the day, and y is and of the day, and z is 3600? i will have 24 slices and want to have JSON like so:
{
floor_id: floor_id
slice: first timestamp of first slice
count: count of devices in this slice
},
{
floor_id: floor_id
slice: first timestamp of second slice
count: count of devices in this slice
},
...
Propably, i need to customise my serializers, using django-filters and write spetial view for this purpose, but i have no ideas how to put it together
UPD: Ok, i customise my serializer for Floor model, and now it looks like:
class FloorWithTPCountSerializer(serializers.ModelSerializer):
class Meta:
model = Floor
fields = ('id', 'count')
count = serializers.SerializerMethodField('get_tp_count')
def get_tp_count(self, obj):
return obj.trackpoint_set.values('tag').distinct().count()
And no i recive JSON like:
{
"id": 28,
"count": 3
},
{
"id": 35,
"count": 1
},
I can suggest, that i need to get querystring params in this serialize class and declarate a method for counting points within timeslice. So, how can i get querydict in serializer class?
Ok, as i suggested early the key was in the serializer customisation. I have to declarete custom method for needed JSON structure.
Here is my solution:
class FloorWithTPCountSerializer(serializers.ModelSerializer):
class Meta:
model = Floor
fields = ('id', 'results')
results = serializers.SerializerMethodField('get_tp_start')
def get_tp_start(self, obj):
query_params = self.context.get("request").query_params
aggregations = {'hour':3600, 'day':86400, 'week':604800}
if 'start' in query_params and 'end' in query_params:
st_tm = int(query_params.get('start'))
en_tm = int(query_params.get('end'))
if 'aggregation' in query_params:
aggregation_value = query_params.get('aggregation')
aggregation = aggregations.get(aggregation_value)
else:
aggregation = en_tm - st_tm
trackpoint_set = obj.trackpoint_set
st = [{'count': trackpoint_set.filter(created_at__gte=ts, created_at__lt=ts + aggregation).values(
'tag').distinct().count(), 'timestamp': ts} for ts in range(st_tm, en_tm, aggregation)]
else:
st = None
return st
Ofcourse, it still lacks some checks by querstring consistency, but i can fetch JSON form, as i needed.
For example, requesting
tpfloors/?start=1496188800&end=1496275199&aggregation=day
i can get something like this:
{
"id": 49,
"results": [
{
"count": 3,
"timestamp": 1496188800
}
]
},
Best regards.

Flask-restful: marshal complex object to json

I have a question regarding flask restful extension. I'm just started to use it and faced one problem. I have flask-sqlalchemy entities that are connected many-to-one relation and I want that restful endpoint return parent entity with all its children in json using marshaller. In my case Set contains many parameters. I looked at flask-restful docs but there wasn't any explanation how to solve this case.
Seems like I'm missing something obvious but cannot figure out any solution.
Here is my code:
# entities
class Set(db.Model):
id = db.Column("id", db.Integer, db.Sequence("set_id_seq"), primary_key=True)
title = db.Column("title", db.String(256))
parameters = db.relationship("Parameters", backref="set", cascade="all")
class Parameters(db.Model):
id = db.Column("id", db.Integer, db.Sequence("parameter_id_seq"), primary_key=True)
flag = db.Column("flag", db.String(256))
value = db.Column("value", db.String(256))
set_id = db.Column("set_id", db.Integer, db.ForeignKey("set.id"))
# marshallers
from flask.ext.restful import fields
parameter_marshaller = {
"flag": fields.String,
"value": fields.String
}
set_marshaller = {
'id': fields.String,
'title': fields.String,
'parameters': fields.List(fields.Nested(parameter_marshaller))
}
# endpoint
class SetApi(Resource):
#marshal_with(marshallers.set_marshaller)
def get(self, set_id):
entity = Set.query.get(set_id)
return entity
restful_api = Api(app)
restful_api.add_resource(SetApi, "/api/set/<int:set_id>")
Now when i call /api/set/1 I get server error:
TypeError: 'Set' object is unsubscriptable
So I need a way to correctly define set_marshaller that endpoint return this json:
{
"id": : "1",
"title": "any-title",
"parameters": [
{"flag": "any-flag", "value": "any-value" },
{"flag": "any-flag", "value": "any-value" },
.....
]
}
I appreciate any help.
I found solution to that problem myself.
After playing around with flask-restful i find out that i made few mistakes:
Firstly set_marshaller should look like this:
set_marshaller = {
'id': fields.String,
'title': fields.String,
'parameters': fields.Nested(parameter_marshaller)
}
Restless marshaller can handle case if parameter is list and marshals to json list.
Another problem was that in API Set parameters has lazy loading, so when i try to marshall Set i got KeyError: 'parameters', so I need explicitly load parameters like this:
class SetApi(Resource):
#marshal_with(marshallers.set_marshaller)
def get(self, set_id):
entity = Set.query.get(set_id)
entity.parameters # loads parameters from db
return entity
Or another option is to change model relationship:
parameters = db.relationship("Parameters", backref="set", cascade="all" lazy="joined")
This is an addition to Zygimantas's answer:
I'm using Flask-RESTful and this is a solution to the loading of the nested properties.
You can pass a callable to the marshal decorator:
class OrgsController(Resource):
#marshal_with(Organization.__json__())
def get(self):
return g.user.member.orgs
Then update the models to return the resource fields for its own entity. Nested entities will thus return the resource fields for its entity relatively.
class Organization(db.Model):
id = db.Column(db.Integer, primary_key=True)
...
#staticmethod
def __json__(group=None):
_json = {
'id': fields.String,
'login': fields.String,
'description': fields.String,
'avatar_url': fields.String,
'paid': fields.Boolean,
}
if group == 'flat':
return _json
from app.models import Repository
_json['repos'] = fields.Nested(Repository.__json__('flat'))
return _json
class Repository(db.Model):
id = db.Column(db.Integer, primary_key=True)
owner_id = db.Column(db.Integer, db.ForeignKey('organization.id'))
owner = db.relationship('Organization', lazy='select', backref=db.backref('repos', lazy='select'), foreign_keys=[owner_id])
...
#staticmethod
def __json__(group=None):
_json = {
'id': fields.String,
'name': fields.String,
'updated_at': fields.DateTime(dt_format='iso8601'),
}
if group == 'flat':
return _json
from app.models import Organization
_json['owner'] = fields.Nested(Organization.__json__('flat'))
return _json
This gives the representation I'm looking for, and honoring the lazy loading:
[
{
"avatar_url": "https://avatars.githubusercontent.com/u/18945?v=3",
"description": "lorem ipsum.",
"id": "1805",
"login": "foobar",
"paid": false,
"repos":
[
{
"id": "9813",
"name": "barbaz",
"updated_at": "2014-01-23T13:51:30"
},
{
"id": "12860",
"name": "bazbar",
"updated_at": "2015-04-17T11:06:36"
}
]
}
]
I like
1) how this approach allows me to define my resource fields per entity and it is available to all my resource routes across the app.
2) how the group argument allows me to customise the representation however I desire. I only have 'flat' here, but any logic can be written and passed down to deeper nested objects.
3) entities are only loaded as necessary.

Categories