can we retrieve the data by sending id field as json object using POST method in django rest framework - django

GET : http://127.0.0.1:8000/r/
{
"count": 5,
"next": null,
"previous": null,
"results": [
{
"id": 1,
"url": "http://127.0.0.1:8000/r/1/",
"name": "India",
"code": 91
},
{
"id": 2,
"url": "http://127.0.0.1:8000/r/3/",
"name": "us",
"code": 10,
},
{
"id": 3,
"url": "http://127.0.0.1:8000/r/4/",
"name": "pak",
"code": 9
},
{
"id": 4,
"url": "http://127.0.0.1:8000/r/5/",
"name": "kansas",
"code": 44
},
{
"id": 5,
"url": "http://127.0.0.1:8000/r/6/",
"name": "kanada",
"code": 123
}
}
I know that , suppose I can get "name": "us", "code": 10 based on it's "id": 2 by specifying the id in the URL like http://127.0.0.1:8000/r/2,
By using GET method,
but I want to retrive "name": "us", "code": 10 based on it's "id": 2 by sending it(means 'id') as json object using 'POST' method.

Related

Loopback Relations Query to update parent

long time lurker in the loopback posts. I'm finally getting around to implementing some CRUD stuff with relations but I'm having some issues.
I have a form model that has a hasMany relation with leverOnes
and levelOnes has a hasMany relation with levelTwos
the individual patches are working fine (for example, if I need to update a single levelOne), but I have a page where the client essentially updates a bunch of levelOnes and levelTwos for a single Form at the same time. I can't update the form with my relation directly.
the patch is taking place at /forms/{id} at the top level, and I am getting a 500 error because when I attempt to patch
"id": "acmeform1",
"name": "ACME Assessment",
"userId": "string",
"org": "Complaints Department",
"clientId": "user#acme.com",
"dueDate": "2022-03-25T18:25:55.734Z",
"readyForReview": true,
"workDriverOnes": [
{
"id": "21740e676035c083fc20004803e88380",
"name": "Governance and Culture",
"description": "Governance and Culture",
"formId": "acmeform1",
"workDriverTwos": [
{
"id": "21740e676035c083fc20004803e8925f",
"name": "Annual Compliance Plan",
"description": "Annual Compliance Plan",
"workDriverOneId": "21740e676035c083fc20004803e88380",
"unitsPerPeriod": 1,
"amount": 20,
"unitType": "Annually",
"ongoing": true,
"roles": [
{
"id": "36c5b6399e951d45a7179f10b1b2df28",
"demandAmount": 5,
"role": "VP - Grade 36 Manager",
"workDriverTwoId": "21740e676035c083fc20004803e8925f"
},
{
"id": "8b054a1cb5002b760d68695b29cf16f6",
"demandAmount": 10,
"role": "SVP - Director",
"workDriverTwoId": "21740e676035c083fc20004803e8925f"
},
{
"id": "b92d2ed082c108d76ee91887234c1b1e",
"demandAmount": 5,
"role": "VP - Grade 37 Manager",
"workDriverTwoId": "21740e676035c083fc20004803e8925f"
}
]
}
]
},
{
"id": "285deab3c703506ac7878327b06eb4bf",
"name": "Policy Governance",
"description": "Policy Governance",
"formId": "acmeform1",
"workDriverTwos": [
{
"id": "8403a66eab00b0de2d2131cfe8d51135",
"name": "Policies",
"description": "Policies",
"workDriverOneId": "285deab3c703506ac7878327b06eb4bf",
"unitsPerPeriod": 1,
"amount": 20,
"unitType": "# Policies",
"ongoing": true,
"roles": [
{
"id": "285deab3c703506ac7878327b06ed443",
"demandAmount": 6,
"role": "VP - Grade 36 Manager",
"workDriverTwoId": "8403a66eab00b0de2d2131cfe8d51135"
},
{
"id": "2e362351b4477cb26f61937a37db27cb",
"demandAmount": 6,
"role": "VP - Grade 37 Manager",
"workDriverTwoId": "8403a66eab00b0de2d2131cfe8d51135"
},
{
"id": "36c5b6399e951d45a7179f10b1b2e932",
"demandAmount": 6,
"role": "SVP - Director",
"workDriverTwoId": "8403a66eab00b0de2d2131cfe8d51135"
},
{
"id": "dd6a475d1ae8f76fc7e7835331a42ad5",
"demandAmount": 2,
"role": "VP - Grade 35 Manager",
"workDriverTwoId": "8403a66eab00b0de2d2131cfe8d51135"
}
]
}
]
},
{
"id": "dd6a475d1ae8f76fc7e7835331a411c6",
"name": "Monitoring",
"description": "Monitoring",
"formId": "acmeform1",
"workDriverTwos": [
{
"id": "dd6a475d1ae8f76fc7e7835331a445a5",
"name": "Monitoring Scripts and Monitoring Plan Development and Drafting",
"description": "Monitoring Scripts",
"workDriverOneId": "dd6a475d1ae8f76fc7e7835331a411c6",
"unitsPerPeriod": 3,
"amount": 1,
"unitType": "# Monitoring Reviews",
"ongoing": true,
"roles": [
{
"id": "36c5b6399e951d45a7179f10b1b2ff49",
"demandAmount": 0.3,
"role": "SVP - Director",
"workDriverTwoId": "dd6a475d1ae8f76fc7e7835331a445a5"
},
{
"id": "9bc41d8a3a9eb3a435d946d8049cc97a",
"demandAmount": 2.7,
"role": "VP - Grade 37 Manager",
"workDriverTwoId": "dd6a475d1ae8f76fc7e7835331a445a5"
}
]
}
]
}
]
}
It says that workDriverOnes is already a relation and I can't update it directly. Do I have to manually loop through my levelOnes and patch each one individually? Or can I batch update at the form level?

How to fetch API data as this. props?

I am trying to fetch data from API in react component as
{this.props.buyer && this.props.buyer[0].phone_number[0].number} - it's throwing error
Cannot read property 'number' of undefined
{this.props.buyer && this.props.buyer[0].name} - it's working fine
This is the API data
Orders: {
buyer:
},
}
[
{
"id": 2,
"name": "Qi Xiang",
"type": "Consignee",
"address": {
"id": 2,
"type": "shipping",
"street": "China China",
"city": "Beijing",
"postal_code": "34343",
"province": "23232",
"country": "CN"
},
"email": null,
"phone_number": {
"number": "323232",
"type": "Phone"
},
"id_image_url": "/api/files/24e49645-df42-4984-a
}
]
},
}
Your phonenumber is not array. You must use this:
this.props.buyer[0].phone_number.number

Django rest framework CRUD API for model with foriegn key elements

Okay so here I am with another problem I'm facing with Django. This time it's the REST framework.
I have a Model with Foreign Key dependencies in my project. I had to retrieve the foreign key field as an object, not just an ID field and was successful in that using Serializer Relations.
So my Serializer.py looks like:
class EventSerializer(serializers.ModelSerializer):
owner = EventUserSerializer(read_only=False)
severity = EventSeveritySerializer(read_only=False)
type = EventTypeSerializer(read_only=False)
cause = EventCauseSerializer(read_only=False)
subcause = EventSubcauseSerializer(read_only=False)
impact = EventImpactSerializer(read_only=False)
point_location = PointLocationSerializer(read_only=False)
class Meta:
model = models.Event
fields = ('id', 'description', 'owner', 'status',
'severity', 'type', 'cause', 'subcause', 'impact',
'is_public', 'occurrence_time', 'reporting_agency',
'estimated_duration', 'confirmed_timestamp',
'rejected_timestamp', 'closed_timestamp', 'point_location', )
Making a GET request, I get the result I wanted:
{
"meta": {
"count": 1,
"previous": null,
"next": null
},
"results": [
{
"id": 8,
"description": "test event 1",
"owner": {
"user": {
"id": 1,
"username": "venus",
"email": "venus.saini#ibigroup.com"
}
},
"status": "confirmed",
"severity": {
"id": 2,
"created": "2016-08-24T07:27:24.722000Z",
"modified": "2016-08-24T07:27:24.723000Z",
"name": "Severe",
"is_enabled": true,
"sortorder": 1
},
"type": {
"id": 2,
"created": "2016-08-24T07:27:45.203000Z",
"modified": "2016-08-24T07:27:45.204000Z",
"name": "Accident",
"event_category": "unplanned",
"sortorder": 2
},
"cause": {
"id": 2,
"created": "2016-08-24T07:28:16.088000Z",
"modified": "2016-08-24T07:28:16.092000Z",
"name": "Whether Conditions",
"sortorder": 3,
"event_type": 2
},
"subcause": {
"id": 2,
"created": "2016-08-24T07:28:28.475000Z",
"modified": "2016-08-24T07:28:28.478000Z",
"name": "Slippery",
"display_name": "Slippery",
"sortorder": 4,
"cause": 2
},
"impact": {
"id": 2,
"created": "2016-08-24T07:28:40.599000Z",
"modified": "2016-08-24T07:28:40.599000Z",
"name": "All Lanes Blocked",
"sortorder": 5
},
"is_public": false,
"occurrence_time": "2016-08-25T06:34:13Z",
"reporting_agency": "",
"estimated_duration": null,
"confirmed_timestamp": "2016-08-25T06:34:15Z",
"rejected_timestamp": "2016-08-25T06:34:16Z",
"closed_timestamp": "2016-08-25T06:34:18Z",
"point_location": {
"id": 2,
"created": "2016-08-24T07:18:03.017000Z",
"modified": "2016-08-24T07:18:03.018000Z",
"roadway_name": "Northbound",
"direction": "North",
"is_primary": false,
"cross_street_name": "test street 1",
"offset": "After",
"distance": 3,
"is_forward": false
}
}]
}
The problem I face is while using the POST request. I don't know how that would work so I found this link. It looks like it has the solution; I mean this is exactly what I want to accomplish [passing the ID of the foreign key element makes the object entry]. But I keep getting random errors like super(BaseSerializer, self).__init__(**kwargs) TypeError: __init__() got an unexpected keyword argument 'is_relation'.
Can somebody please guide me to how I should be doing it? I'm using Postman to test my APIs.
Thanks in advance.
EDIT: Using PUT/POST without the CustomSerializer gives me error 415:
{
"detail": "Unsupported media type \"text/plain;charset=UTF-8\" in request."
}

Facebook Graph Api: Unable to get latest post for keywords

Can anyone help me, When I request using link
https://graph.facebook.com/search?&limit=2&until=now&q=hi&type=post&access_token=AAAAAAITEghMBAIScEzst3h5VpV8pKZA7di2ZC3czxr5
Note: I am not giving valid token for security reason.
I always get the result for 27 Nov 2012, while I tried to search for latest post. Am I missing something? Please help.
Thanks in Advance.
RESULT I get:
{
"data": [
{
"id": "730221920_10151138508071921",
"from": {
"name": "Pinky Saowichit",
"id": "730221920"
},
"message": "\u0e2d\u0e32\u0e17\u0e34\u0e15\u0e22\u0e4c\u0e19\u0e35\u0e49\u0e2d\u0e32\u0e08\u0e44\u0e21\u0e48\u0e44\u0e14\u0e49\u0e2d\u0e2d\u0e31\u0e19\u0e1a\u0e48\u0e2d\u0e22\u0e46 \u0e19\u0e30\u0e04\u0e30 \u0e41\u0e1f\u0e19\u0e04\u0e25\u0e31\u0e1a \u0e43\u0e2b\u0e49\u0e23\u0e39\u0e49\u0e27\u0e48\u0e48\u0e32\u0e04\u0e34\u0e14\u0e16\u0e36\u0e07\u0e19\u0e30\u0e04\u0e30 \u0e41\u0e15\u0e48\u0e15\u0e2d\u0e19\u0e19\u0e35\u0e49\u0e40\u0e23\u0e32\u0e01\u0e33\u0e25\u0e31\u0e07\u0e04\u0e34\u0e14\u0e16\u0e36\u0e07\u0e04\u0e38\u0e13 \u0e04\u0e32\u0e19\u0e18\u0e35\u0e41\u0e25\u0e30\u0e40\u0e17\u0e40\u0e23\u0e0b\u0e48\u0e32\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e21\u0e32\u0e01\u0e46 \u0e2b\u0e38 \u0e2b\u0e38",
"privacy": {
"value": ""
},
"type": "status",
"status_type": "mobile_status_update",
"created_time": "2012-11-27T03:12:32+0000",
"updated_time": "2012-11-27T07:44:54+0000",
"likes": {
"data": [
{
"name": "Weniefredo Laud",
"id": "100004751890183"
},
{
"name": "\u0e1e\u0e23\u0e15 \u0e1c\u0e25\u0e14\u0e35",
"id": "100001056963372"
},
{
"name": "\u0e08\u0e23\u0e34\u0e0d\u0e32 \u0e25\u0e2d\u0e27",
"id": "100000046347271"
},
{
"name": "Noolek Tikamborn",
"id": "100003048464258"
}
],
"count": 6
}
},
{
"id": "100003971930240_283727615063091",
"from": {
"name": "Mariana Salcedo",
"id": "100003971930240"
},
"story": "Mariana Salcedo shared a link.",
"picture": "http://external.ak.fbcdn.net/safe_image.php?d=AQASxQms6c0xFkdO&w=90&h=90&url=http\u00253A\u00252F\u00252Fask.fm\u00252Fimages\u00252F50x50.gif",
"link": "http://ask.fm/marianasalcedo/answer/15735435637",
"name": "CRUSH hi",
"caption": "jajajajaja que enfadosa erees!",
"icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yN/x/aS8ecmYRys0.gif",
"privacy": {
"value": ""
},
"type": "link",
"status_type": "shared_story",
"application": {
"name": "Ask.fm",
"id": "129215213762342"
},
"created_time": "2012-11-27T03:11:38+0000",
"updated_time": "2012-11-27T03:11:38+0000"
}
],
"paging": {
"previous": "https://graph.facebook.com/search?q=hi&limit=2&type=post&access_token=AAAAAAITEghMBAIScEzst3h5VpV8pKZA7di2ZC3czxr5ANnvKPPc1wqYJJZAANd9PI0NKLX5Xk5ReRX3T01iim8BoJp6r16itU8vPxRP&since=1353985952&__previous=1",
"next": "https://graph.facebook.com/search?q=hi&limit=2&type=post&access_token=AAAAAAITEghMBAIScEzst3h5VpV8pKZA7di2ZC3czxr5ANnvKPPc1wqYJJZAANd9PI0NKLX5Xk5ReRX3T01iim8Bo&until=1353985897"
}
}

Is the referral (fb_action_ids) value, stored within the activity made by a user on a timeline app?

I'm testing a timeline app I just made.
I'm using Graph API Explorer for testing.
I tested the next URL:
https://graph.facebook.com/10150691128476714
where 10150691128476714 is the id of the activity I made through my app.
It returns the next values:
{
"id": "10150691128476714",
"from": {
"name": "Moisés Briseño Estrello",
"id": "719621713"
},
"start_time": "2012-04-26T07:22:00+0000",
"end_time": "2012-04-26T07:22:00+0000",
"publish_time": "2012-04-26T07:22:00+0000",
"application": {
"name": "appname",
"namespace": "appname",
"id": "367747679937600"
},
"data": {
"objectname": {
"id": "10150825002710211",
"url": "https://young-planet-1642.herokuapp.com/AMLO.html",
"type": "appname:objectname",
"title": "TItle"
}
},
"type": "appname:actionname",
"likes": {
"count": 0,
"can_like": true,
"user_likes": false
},
"comments": {
"count": 0,
"can_comment": true
}
}
I wonder if the referral (fb_action_ids) value is also stored in here or where I can find it?
Thanks in advance :)
ID of the action is the fb_action_ids value, i.e. fb_action_ids = 10150691128476714.