How to group_by Queryset distinct by id and append values Django - django

In Django i have the results i want but it returns separated data in same ids
How to groupby ids in a list of dicts?
Use pandas to fix this, but it doesn't work quite right. What I need is a simple dictionary list where if there is a repeated id, the information that is different is added as the value of that key and if there are several values, it is stored in a list. So as I show below in the result I want
i have this:
<
QuerySet[{
'id': 7086098,
'action_plan': None,
'comment': None
}, {
'id': 7105838,
'action_plan': 'foo',
'comment': None
}, {
'id': 7105838,
'action_plan': 'foos2',
'comment': None
}, {
'id': 7169339,
'action_plan': 'xxxxxx',
'comment': None
}, {
'id': 7169346,
'action_plan': 'report',
'comment': None
}, {
'id': 7169346,
'action_plan': 'zxczxczxczc',
'comment': None
}, {
'id': 7622793,
'action_plan': 'foofoo',
'comment': None
}, {
'id': 7622793,
'action_plan': 'role play',
'comment': None
}, {
'id': 7723661,
'action_plan': 'google',
'comment': 'chrome'
}, {
'id': 7723661,
'action_plan': 'netscape',
'comment': None
}, {
'id': 7723661,
'action_plan': 'urra',
'comment': 'firefox'
}, {
'id': 7723661,
'action_plan': 'sdasd',
'comment': None
}] >
i want to get this:
[{
'id': 7086098,
'action_plan': None,
'comment': None
}, {
'id': 7105838,
'action_plan': ['foo', 'foos2'],
'comment': [None, None]
}, {
'id': 7169339,
'action_plan': 'xxxxxx',
'comment': None
}, {
'id': 7169346,
'action_plan': ['report', 'zxczxczxczc'],
'comment': [None, None]
}, {
'id': 7622793,
'action_plan': ['foofoo', 'role play'],
'comment': [None, None]
}, {
'id': 7723661,
'action_plan': ['google', 'netscape', 'urra', 'sdasd'],
'comment': ['chrome', None, 'firefox', None]
}]

Related

Django Annotate with one model and include "annotated" records in queryset

I am fairly new to Django and have figured out the "basic" annotate function, but I would like to include the records from the annotated values. For example,
qs = People.objects.values('status').annotate(total=Count('id'),totalScore=Sum('score'),averageAge=Avg('age'))
I would like to include the summarized records in the result, in this case the individual people, like so...
[
{'status': 'One',
'total': 2,
'totalScore': 150,
'averageAge': 36,
'people': [
{
'id': 1,
'status': 'One',
'name': 'Jim',
'score': 80,
'age': 40
},
{
'id': 5,
'status': 'One',
'name': 'Jill',
'score': 70,
'age': 32
}
]},
{'status': 'Two',
'total': 1,
'totalScore': 85,
'averageAge': 42,
'people': [
{
'id': 3,
'status': 'Two',
'name': 'Jack',
'score': 85,
'age': 42
}
]
},...
Thanks for any help.

Django_jsonform not detecting changes, when attempting to makemigrations

I've added explanation to JSON form but when i attempt python manage.py makemigrations questions, says no changes detected in app "questions" I've added questions to INSTALLED_APPS in settings.py
This only happens when i edit the JSON form. If i edit outside, then makemigrations work?
class NewQuiz(models.Model):
ITEMS_SCHEMA = {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'question_english': { 'type': 'string' },
'answers': {
'type': 'array',
'items': {
'type': 'object',
'properties': {
'answer_english': { 'type': 'string' },
'is_correct': {
'type': 'boolean',
'title': 'Is correct',
'default': False,
}
}
}
},
'explanation_english': { 'type': 'string' },
}
}
}
How do i solve?

Check if a set of key/values in dict list is in another list

I have a list as follows:
[
{
'Name': 'Name1',
'Address': 'Address1'
},
{
'Name': 'Name2',
'Postcode': 'MyPostcode'
}
]
And another list like this:
[
{
'Name': 'Name1',
'data' : {
'Address': 'Address1',
'Postcode': 'Whatever'
},
{
'Name': 'Name2',
'data' : {
'Address': 'Whatever',
'Postcode': 'MyPostcode'
},
{
'Name': 'Name3',
'data' : {
'Address': 'Whatever',
'Postcode': 'Whatever'
},
]
For any of the items in the first list, I need to check if that combination of key/value exists in the second, and if so, delete it from the second
I can do it in multiple lines of codes, with different for loops, but probably there is a wiser way of doing it. Can anybody suggest an elegant solution?
In the example above, it should delete the two first dicts from the list and return a list only with the third one
[
{
'Name': 'Name3',
'data' : {
'Address': 'Whatever',
'Postcode': 'Whatever'
}
]
Thanks
Try:
lst1 = [
{"Name": "Name1", "Address": "Address1"},
{"Name": "Name2", "Postcode": "MyPostcode"},
]
lst2 = [
{"Name": "Name1", "data": {"Address": "Address1", "Postcode": "Whatever"}},
{
"Name": "Name2",
"data": {"Address": "Whatever", "Postcode": "MyPostcode"},
},
{"Name": "Name3", "data": {"Address": "Whatever", "Postcode": "Whatever"}},
]
out = []
for d2 in lst2:
t = set({"Name": d2["Name"], **d2["data"]}.items())
for d1 in lst1:
if t.issuperset(d1.items()):
break
else:
out.append(d2)
print(out)
Prints:
[{'Name': 'Name3', 'data': {'Address': 'Whatever', 'Postcode': 'Whatever'}}]

Dart/Flutter - How to delete key-value-pairs of element of Map inside a List?

How can I can delete a specific key-value-pair in that type of List<Map<String, dynamic>> for each map.
For example:
List<Map<String, dynamic>> before the operation:
List<Map<String, dynamic>> currencies = [
{
'id': 1,
'name': 'coin',
'desc': 'This is just a blindtext.',
'order': 101,
'icon': 'https://icon1.jpg'
},
{
'id': 2,
'name': 'karma',
'desc': 'This is just a blindtext.',
'order': 102,
'icon': 'https://icon2.jpg'
},
{
'id': 3,
'name': 'laurel',
'desc': 'This is just a blindtext.',
'order': 104,
'icon': 'https://icon3.jpg'
},
];
List<Map<String, dynamic>> after the operation I am searching for:
List<Map<String, dynamic>> currencies = [
{
'id': 1,
'name': 'coin',
'icon': 'https://icon1.jpg'
},
{
'id': 2,
'name': 'karma',
'icon': 'https://icon2.jpg'
},
{
'id': 3,
'name': 'laurel',
'icon': 'https://icon3.jpg'
},
];
So basically, I deleted "unwanted" key-value-pairs.
Any suggestions?
Thanks in advance!!
currencies.forEach((item) => item..remove("order")..remove("desc"));

GAE Recursive Query

I have some code (below) that is producing valid results and overall I am happy with it. However, it's quite 'wordy' and I am interested to know if this good/bad approach - this there something more effective/simpler that I should be doing?
I am really pleased to have the code in the model and not in my api, so I would like to keep it that way.
class ndb_Project(ndb.Model):
name = ndb.StringProperty()
description = ndb.StringProperty()
version = ndb.StructuredProperty(ndb_Version)
parentProj = ndb.KeyProperty()
childProj = ndb.KeyProperty(repeated=True)
#classmethod
def getChildProjects(cls,key):
proj = key.get()
a = []
for c in proj.childProj:
a.append(proj.getChildProjects(c))
o = proj.to_dict()
o['urlSafe'] = proj.key
o['childProj'] = a
return o
Many thanks
Toby
An alternative?
#classmethod
def getChildProjects(cls, proj):
o = proj.to_dict()
o['urlSafe'] = proj.key
temp = []
a = ndb.get_multi(proj.childProj) if proj.childProj else []
for c in a:
temp.append(proj.getChildProjects(c))
o['childProj']
return o
Something like this should work using ndb.get_multi(). It will also be faster since it fetches multiple keys in a single rpc instead of serially like your current loop.
Edit I ran this in /_ah/stats/shell so I know it works as long as it returns the output you desire.
from google.appengine.ext import ndb
from pprint import pprint as pp
class ndb_Project(ndb.Model):
name = ndb.StringProperty()
description = ndb.StringProperty()
parentProj = ndb.KeyProperty()
childProj = ndb.KeyProperty(repeated=True)
def getChildProjects(self):
a = [ent.getChildProjects() for ent in ndb.get_multi(self.childProj)]
o = self.to_dict()
o['urlSafe'] = self.key
o['childProj'] = a
return o
p = ndb_Project(name='first', description='first entity')
p.put()
for x in xrange(5):
tmp = ndb_Project(name=str(x), description='desc %s' % x)
p.childProj.append(tmp.put())
for y in xrange(5):
tmp2 = ndb_Project(name='%s %s' % (x, y), description='desc %s %s' % (x, y))
tmp.childProj.append(tmp2.put())
tmp.put()
p.put()
pp(p.getChildProjects())
output
{'childProj': [{'childProj': [{'childProj': [],
'description': u'desc 0 0',
'name': u'0 0',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5251250308251648)},
{'childProj': [],
'description': u'desc 0 1',
'name': u'0 1',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5245618633048064)},
{'childProj': [],
'description': u'desc 0 2',
'name': u'0 2',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5793979555643392)},
{'childProj': [],
'description': u'desc 0 3',
'name': u'0 3',
'parentProj': None,
'urlSafe': Key('ndb_Project', 6371518539890688)},
{'childProj': [],
'description': u'desc 0 4',
'name': u'0 4',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5208529711398912)}],
'description': u'desc 0',
'name': u'0',
'parentProj': None,
'urlSafe': Key('ndb_Project', 6460121467060224)},
{'childProj': [{'childProj': [],
'description': u'desc 1 0',
'name': u'1 0',
'parentProj': None,
'urlSafe': Key('ndb_Project', 6334429618241536)},
{'childProj': [],
'description': u'desc 1 1',
'name': u'1 1',
'parentProj': None,
'urlSafe': Key('ndb_Project', 6344957925261312)},
{'childProj': [],
'description': u'desc 1 2',
'name': u'1 2',
'parentProj': None,
'urlSafe': Key('ndb_Project', 6448551898906624)},
{'childProj': [],
'description': u'desc 1 3',
'name': u'1 3',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5286432096649216)},
{'childProj': [],
'description': u'desc 1 4',
'name': u'1 4',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5808107145920512)}],
'description': u'desc 1',
'name': u'1',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5282008817205248)},
{'childProj': [{'childProj': [],
'description': u'desc 2 0',
'name': u'2 0',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5808889098403840)},
{'childProj': [],
'description': u'desc 2 1',
'name': u'2 1',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5035775120900096)},
{'childProj': [],
'description': u'desc 2 2',
'name': u'2 2',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5881052870475776)},
{'childProj': [],
'description': u'desc 2 3',
'name': u'2 3',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5850614068150272)},
{'childProj': [],
'description': u'desc 2 4',
'name': u'2 4',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5215350924771328)}],
'description': u'desc 2',
'name': u'2',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5811114428334080)},
{'childProj': [{'childProj': [],
'description': u'desc 3 0',
'name': u'3 0',
'parentProj': None,
'urlSafe': Key('ndb_Project', 6368218830602240)},
{'childProj': [],
'description': u'desc 3 1',
'name': u'3 1',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5287664114728960)},
{'childProj': [],
'description': u'desc 3 2',
'name': u'3 2',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5041177015353344)},
{'childProj': [],
'description': u'desc 3 3',
'name': u'3 3',
'parentProj': None,
'urlSafe': Key('ndb_Project', 6412332003491840)},
{'childProj': [],
'description': u'desc 3 4',
'name': u'3 4',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5231029602222080)}],
'description': u'desc 3',
'name': u'3',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5245939144982528)},
{'childProj': [{'childProj': [],
'description': u'desc 4 0',
'name': u'4 0',
'parentProj': None,
'urlSafe': Key('ndb_Project', 4964143656337408)},
{'childProj': [],
'description': u'desc 4 1',
'name': u'4 1',
'parentProj': None,
'urlSafe': Key('ndb_Project', 4927054734688256)},
{'childProj': [],
'description': u'desc 4 2',
'name': u'4 2',
'parentProj': None,
'urlSafe': Key('ndb_Project', 6348349238149120)},
{'childProj': [],
'description': u'desc 4 3',
'name': u'4 3',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5004957119938560)},
{'childProj': [],
'description': u'desc 4 4',
'name': u'4 4',
'parentProj': None,
'urlSafe': Key('ndb_Project', 4960843947048960)}],
'description': u'desc 4',
'name': u'4',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5250989925859328)}],
'description': u'first entity',
'name': u'first',
'parentProj': None,
'urlSafe': Key('ndb_Project', 5208229332123648)}
original code so that the original comments to this answer make sense
class ndb_Project(ndb.Model):
name = ndb.StringProperty()
description = ndb.StringProperty()
version = ndb.StructuredProperty(ndb_Version)
parentProj = ndb.KeyProperty()
childProj = ndb.KeyProperty(repeated=True)
#classmethod
def getChildProjects(cls, key):
proj = key.get()
a = ndb.get_multi(proj.childProj) if proj.childProj else []
o = proj.to_dict()
o['urlSafe'] = proj.key
o['childProj'] = a
return o