I have created created a dictionary from Django-mptt and used json.dump() to get the JSON object.
{
"a": "cat1",
"c": ["item2", "item1"],
"b": [
{"a": "burgers", "c": [], "b": []},
{"a": "south indian", "c": [], "b": []},
{"a": "veg subs", "c": ["corn and peas", "a;loo patty", "paneer delite"], "b": []},
{"a": "traditional", "c": ["subway melt", "Chicken ranch", "turkey", "Subway club"], "b": []},
{"a": "favourites", "c": ["tuna", "chicken ham", "roasted chicken"], "b": []},
{"a": "beverages", "c": [], "b": []},
{"a": "north indian", "c": [], "b": []},
{"a": "oriental", "c": [], "b": []},
{"a": "european ", "c": [], "b": []}
]
}
Here, "cat1" is the main category, having sub-categories children, burgers, traditional, favourites etc. Any category can have sub-categories.
a - Category name
c - Items list
b - Children
This is my JSON object. What do I use to show this JSON object in tree format in JavaScript.
I need to display the First children of the every category clicked. And for every Category clicked, I want to display the items below the tree structure.
Related
I have a dictionary that contains a list of dictionaries, of which one of the elements (C) contains another list of dictionaries. The structure is as follows:
{
"fruits":
[
{
"A": "apple",
"B": "banana",
"C":
[
{
"D": "Green grape",
"E": "Red grape",
}
{
"D": "Orange",
"E": "Grapefruit",
}
]
}
],
"vegetables":
etc...
}
I would like to move the elements D & E up a hierarchy while removing C. The resulting dictionary should look as follows.
{
"fruits":
[
{
"A": "apple",
"B": "banana",
"D": "Green grape",
"E": "Red grape",
},
{
"A": "apple",
"B": "banana",
"D": "Orange",
"E": "Grapefruit",
}
]
"vegetables":
etc...
}
Any suggestions?
I am trying to pass the data to a serializer like the following:
myDict = {
"invoice_date": "2021-02-24T11:44:13+05:30",
"invoice_number": "12",
"vendor": "4",
"amount": "12",
"gst": "12",
"total_amount": "14",
"transaction_type": "Allot",
"status": "Hold",
"transactions": [
{
"t_no": 47,
"f_mile": "45",
"long_haul": "45",
"l_mile": "45",
"labour": "45",
"others": "54",
"a_t_no": 47,
},
{
"t_no": 102,
"f_mile": "12",
"long_haul": "12",
"l_mile": "21",
"labour": "21",
"others": "21",
"a_t_no": 102,
},
],
"owner": 2,
}
But when I check the validated data in the serialzer it shows it without the transactions data:
{'invoice_date': datetime.datetime(2021, 2, 24, 6, 14, 13, tzinfo=<UTC>), 'invoice_number': '12', 'amount': 12, 'gst': 12, 'total_amount': 14, 'status': 'Hold', 'transaction_type': 'Allot', 'vendor': <Vendor: Vendor object (4)>, 'owner': <User: yantraksh>}
so I tried to check the initial data that is being passed to the serializer :
<QueryDict: {
"invoice_date": ["2021-02-24T11:44:13+05:30"],
"invoice_number": ["12"],
"vendor": ["4"],
"amount": ["12"],
"gst": ["12"],
"total_amount": ["14"],
"transaction_type": ["Allot"],
"status": ["Hold"],
"transactions": [
'[{"t_no":47,"f_mile":"45","long_haul":"45","l_mile":"45","labour":"45","others":"54","a_t_no":47},{"t_no":102,"f_mile":"12","long_haul":"12","l_mile":"21","labour":"21","others": "21","a_t_no":102}]'
],
"owner": [2],
}>
It shows that the transaction data is being passed as a string, what should I change it to in order to get it as validated data ?
Based on the "evidence" in that QueryDict (namely strings wrapped in lists) it sounds like you're not submitting the data as JSON.
You can't use regular HTML form data to submit structured data, you'll need to post JSON data.
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.
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
http://www.django-rest-framework.org/api-guide/relations/#nested-relationships
It is not clear from the documentation if and how it's possible to use the same principle of nested relationships to render a flat JSON.
For example:
"nest": {
"b": {
"c": {
"d": {
"e": {
"E": "echo"
},
"D": "delta"
},
"C": "charlie"
},
"B": "beta"
},
"A": "alpha"
}
"flat": {
"A": "alpha",
"B": "beta",
"C": "charlie",
"D": "delta",
"E": "echo"
}
How can the flat JSON be achieved?
Lets assume your code looks like
mymodel_queryset = MyModel.objects.all()
So what you can do is write your own custom serializer as follows:
def my_custom_serializer(queryset):
res = []
for q in queryset:
ob = {'name': q.name, 'city': q.city.name} # Notice how i use the relation here for city. So the nested relation is becoming flat for city
res.append(ob)
return res
Now in your code you can use
data = my_custom_serializer(mymodel_queryset)
return Response(status=status.HTTP_200_OK, data=data)
Hope this helps you. :)