How to pass json data as queryset object to django template? - django

I have this json data in views.py
[
{
"id": 6,
"first_name": "Star",
"last_initial": "W",
"phone": "+918893972142",
"email": "star#gmail.com",
"date_created": "2020-10-12T17:17:17.629123Z",
"orders": []
},
{
"id": 4,
"first_name": "Sam",
"last_initial": "0",
"phone": "+918766897214",
"email": "sam#gmail.com",
"date_created": "2020-10-12T17:13:33.435065Z",
"orders": []
},
{
"id": 3,
"first_name": "Gaara",
"last_initial": "W",
"phone": "+918668972789",
"email": "gaara#gmail.com",
"date_created": "2020-10-12T17:08:44.906809Z",
"orders": [
{
"order_id": "9",
"customer_id": "3",
"customer_name": "Gaara W",
"product_id": "3",
"product_name": "Jet",
"date_created": "2020-10-12T17:18:18.823289Z",
"status": "Delivered"
}
]
}
]
I want to pass this data as a context object to the template from views using render function. so I can display this data using {% for %} loop in my template.

Pass the data into the template.
return render(request, "app/template.html", {
"data": data
})
Then you can use for loops to render the data you need.

Related

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

Elastic Search Sort

I have a table for some activities like
[
{
"id": 123,
"name": "Ram",
"status": 1,
"activity": "Poster Design"
},
{
"id": 123,
"name": "Ram",
"status": 1,
"activity": "Poster Design"
},
{
"id": 124,
"name": "Leo",
"categories": [
"A",
"B",
"C"
],
"status": 1,
"activity": "Brochure"
},
{
"id": 134,
"name": "Levin",
"categories": [
"A",
"B",
"C"
],
"status": 1,
"activity": "3D Printing"
}
]
I want to get this data from elastic search 5.5 by sorting on field activity, but I need all the data corresponding to name = "Ram" first and then remaining in a single query.
You can use function score query to boost the result based on match for the filter(this case ram in name).
Following query should work for you
POST sort_index/_search
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"boost": "5",
"functions": [{
"filter": {
"match": {
"name": "ram"
}
},
"random_score": {},
"weight": 1000
}],
"score_mode": "max"
}
},
"sort": [{
"activity.keyword": {
"order": "desc"
}
}]
}
I would suggest using a bool query combined with the should clause.
U will also need to use the sort clause on your field.

Django - Search last object created for every id and keep sum off other field

I'm doing a query where I want to get the list of all Games, related to an specific Team, but I want the field "date_created", to save only the last date.
So here is my query:
"games": [
{
"id": 3,
"first_name": "Odille",
"last_name": "Adamovitz",
"date_created": "2017-08-24T00:00:00",
"points": "10",
},
{
"id": 3,
"first_name": "Odille",
"last_name": "Adamovitz",
"date_created": "2017-09-18T00:00:00",
"points": "10",
},
{
"donation__sponsor": 3,
"first_name": "Odille",
"last_name": "Adamovitz",
"date_created": "2016-06-20T00:00:00",
"points": "10",
},
{
"id": 5,
"first_name": "Bail",
"last_name": "Brownbill",
"date_created": "2017-11-10T00:00:00",
"points": "10",
},
{
"id": 5,
"first_name": "Bail",
"last_name": "Brownbill",
"date_created": "2018-01-31T00:00:00",
"points": "10",
}
]
And my desire query is:
"games": [
{
"id": 3,
"first_name": "Odille",
"last_name": "Adamovitz",
"date_created": "2017-09-18T00:00:00",
"points": "10",
},
{
"id": 5,
"first_name": "Bail",
"last_name": "Brownbill",
"date_created": "2018-01-31T00:00:00",
"points": "10",
}
]
My final objective with this is to have a query with another field that sums an attribute inside
Something like this:
"games": [
{
"id": 3,
"first_name": "Odille",
"last_name": "Adamovitz",
"date_created": "2017-08-24T00:00:00",
"points": "30",
},
{
"id": 5,
"first_name": "Bail",
"last_name": "Brownbill",
"date_created": "2018-01-31T00:00:00",
"points": "20",
}
]
Here is my closest idea:
#I get the sum of points with the name, but missing last date_created
qs = Game.filter(id=1).values('first_name', 'last_name', 'date_created').annotate(points = Sum('points')
With .latest() I'm only getting the last Game, not the last game related to every id.
I thought that I need to do two querysets, and then somekind of union, but idk
Try this,
from django.db.models import Sum, Max
Game.objects.values('id').annotate(points=Sum('point'), date_created=Max('date_created'))
This form is equivalant to raw sql,
Select id,SUM(point) as points, MAX(date_created) as date_created from GameTable Group By id

Get Malformed error while patch request to update payment

I want to reduce some items of my order from payment. That's why I'm using replace method of PayPal python API that allows to update or add data after an object of payment has been created.
Error:
{u'message': u'Incoming JSON request does not map to API request', u'debug_id': u'd57742e9d3b03', u'name': u'MALFORMED_REQUEST', u'information_link': u'https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST'}
Code:
create payment:
I added two products while creating a payment object.
payment = Payment({
"intent": "sale",
"payer": {
"payment_method": "paypal"},
"redirect_urls": {
"return_url": "http://localhost:3000/payment/execute",
"cancel_url": "http://localhost:3000/"},
"transactions": [{
"item_list": {
"items": [{
"name": "Ice-cream",
"sku": "I10001",
"price": "50.00",
"currency": "USD",
"quantity": 12},
{
"name": "Ice-cream12",
"sku": "I100012",
"price": "60.00",
"currency": "USD",
"quantity": 12}
]},
"amount": {
"total": "1320.00",
"currency": "USD"},
"description": "This is the payment transaction description."}]})
payment.create()
Update Payment:
update_payment_json = [
{
"op":"replace",
"path":"/transactions/0/item_list",
"values":{"items":[{
"name": "Ice-cream",
"sku": "I10001",
"price": "50.00",
"currency": "USD",
"quantity": 12,
},]}
},
{
"op":"replace",
"path":"/transactions/0/amount",
"value":
{
"total": "600.00",
"currency": "USD",
"details":{
"subtotal": "600.00",
"shipping":"0.0",
}},
},
]
payment.replace(update_payment_json)
I don't believe the below path is sufficient to determine which item are you updating ? there are 2 items in the item_list correct? if you are updating just 1 of it then you need to qualify it to the right path so that you are clear about which item you are replacing.
"path":"/transactions/0/item_list",

Django Rest Framework Views in Depth

How to setup the views or serializers in Django Rest Framework to have URLs for depth to add, modify
articles with categories and tags.
/api/article/92285/categories
/api/article/92285/tags
from following format
{
"article_id": 92285,
"views": 0,
"downloads": 0,
"shares": 0,
"handle_url": "",
"title": "Test dataset",
"defined_type": "dataset",
"status": "Drafts",
"published_date": "",
"description": "Test description",
"total_size": 0,
"owner": {
"id": 13483,
"full_name": "John Carter"
},
"authors": [
{
"first_name": "John ",
"last_name": "Carter",
"id": 13483,
"full_name": "John Carter"
}
],
"tags": [
],
"categories": [
],
"files": [
]
}