Django annotate(),Count() - django

I get the output like this using annotate() and Count()
<QuerySet [{'pid': 11, 'status': 'Completed', 'status__count': 3}, {'pid': 11, 'status': 'Hold', 'status__count': 12}, {'pid': 11, 'status': 'InProgress', 'status__count': 2}, {'pid': 11, 'status': 'New', 'status__count': 3}, }]
this is the code I write to get like this
view.py:
tasks = Task.objects.values('pid','status').annotate(Count('status')).order_by('pid')
Actually I want my output like this
<QuerySet [{'pid': 11, 'Completed': 3, 'Hold': 12, 'InProgress': 2,'New': 3},}]
How can I do it?

You can try like this:
Task.objects.values('pid').annotate(
completed = Count('status', filter=Q(status='Completed')),
hold=Count('status', filter=Q(status="Hold")),
in_progress=Count('status', filter=Q(status="InProgress"))
).order_by('pid')
More information can be found in documentation.

An another method i found this also worked
Thanks you

Related

How to get video links from subfolders in vimeo API?

I am trying vimeo API using python in a jupyter notebook.
I want to access the links of videos located inside subfolders of a parent folder.
I can get the links of all my videos with GET /me/videos?fields=link... but if I try setting the parent folder in the uri I don't get any video (since in that folder there are none)
I know I can use "include_subfolders" with the query parameter (or if I had a tag...), but I want all the results, not to query by any particular word. I have tried querying a letter and I get some of the videos, but never all of them, and that cannot be the best solution.
Is there a way to query ALL videos inside all subfolders? I have tried with "",*, ...nothing seems to work.
Another possible solution I have thought of would be to search all the videos and filter by parent folder. With the metadata.connections.ancestor_path I get an array that I could later manipulate... but I have too many videos and I don't want them all at once, I want to get the links by folders, if possible.
Things I tried but didn't work:
In a folder with 8 subfolders and 30 video links I tried with items :
uri='https://api.vimeo.com/users/123456789/projects/3213121/items'
video_data = client.get(uri + '?fields=created_time,name,link,duration&sort=date&per_page=100&page=1').json()
Result:
{'total': 8, 'page': 1, 'per_page': 100, 'paging': {'next': None, 'previous': None, 'first': '/users/123456789/projects/3213121/items?fields=created_time%2Cname%2Clink%2Cduration&sort=date&per_page=100&page=1', 'last': '/users/123456789/projects/3213121/items?fields=created_time%2Cname%2Clink%2Cduration&sort=date&per_page=100&page=1'}, 'data': [[], [], [], [], [], [], [], []]}
Example of a query including subfolders, instead of a word, by letter:
#parent folder:
uri="https://api.vimeo.com/me/projects/12345678/videos"
video_data = client.get(uri + '?fields=created_time,name,link,duration&sort=date&per_page=100&page=1&query=A&include_subfolders=true').json()
Result: (not all the links I wanted, but some, so it should not be a problem of permisions or privacy...)
{'total': 12, 'page': 1, 'per_page': 100, 'paging': {'next': None, 'previous': None, 'first': '/me/folders/5745971/videos?fields=created_time%2Cname%2Clink%2Cduration&sort=date&per_page=100&page=1&query=A&include_subfolders=true', 'last': '/me/folders/12345678/videos?fields=created_time%2Cname%2Clink%2Cduration&sort=date&per_page=100&page=1&query=A&include_subfolders=true'}, 'data': [{'name': 'Circuito a TV', 'link': 'https://vimeo.com/12345678', 'duration': 520, 'created_time': '2021-09-22T21:38:04+00:00'}, {'name': '3 TV a Álgebra', 'link': 'https://vimeo.com/12345678', 'duration': 338, 'created_time': '2021-09-22T11:07:06+00:00'}, {'name': '2 Circuito a Álgebra', 'link': 'https://vimeo.com/12345678', 'duration': 681, 'created_time': '2021-09-22T11:06:04+00:00'}, {'name': '5 Álgebra a circuito', 'link': 'https://vimeo.com/12345678', 'duration': 267, 'created_time': '2021-09-22T11:11:34+00:00'}
This is my first question and I have searched but haven't found any question already talking about this exact issue, hope I made myself more or less clear.

Marshaling items in lists using flask restful

I am writing a Flask-RESTFUL resource. It returns a Model object called DocumentSet with the following structure:
DocumentSet:
id: int
documents: list of Document
Document is another Model object with the following structure:
Document:
id: int
...other fields...
I want to write a #marshal_with decorator that returns the DocumentSet id along with a list of the Document ids like so:
{
id: 5,
document_ids: [1, 2, 3]
}
I've been banging my head against the output marshaling syntax to no avail. Some of the things I've tried:
{'id': fields.Integer, 'document_ids':fields.List(fields.Integer, attribute='documents.id')}
{'id': fields.Integer, 'document_ids':fields.List(fields.Nested({'id':fields.Integer}), attribute='documents')}
What's the magic incantation?
The magic incantation is
{'id': fields.Integer, 'name': fields.String, 'document-ids':{'id': fields.Integer}}
It was right there in the "Complex Structures" paragraph in the documentation.

Space Efficient Django model type to store large amount of text

currently I'm trying to port existing Google App Engine application from webapp2 to django using the djangoappengine.
Are there a equivalent in memory space saving ways to store the data using Django? Because there are limits to the amount stored in GAE for free user.
webapp2 model code
class TagTrend_refine(ndb.Model):
tag = ndb.StringProperty()
trendData = ndb.BlobProperty(compressed=True)
I know that TextField can store large amount of text, but can it store using lesser memory? Is using BlobField possible?
An example of data being store for trendData (as many as 24783 characters) is
{"2008": "{\"nodes\": [{\"group\": 0, \"name\": \"ef-code-first\", \"degree\": 6}, {\"group\": 1, \"name\": \"gridview\", \"degree\": 6}, {\"group\": 2, \"name\": \"mvvm\", \"degree\": 6}, {\"group\": 1, \"name\": \"webforms\", \"degree\": 6}, {\"group\": 2, \"name\": \"binding\", \"degree\": 6}, {\"group\": 3, \"name\": \"web-services\", \"degree\": 6}, {\"group\": 2, \"name\": \"datagrid\", \"degree\": 6},...
Django itself doesn't natively have a way to store data compressed, however you could use the zlib module to compress data before saving it to the database.
Here's a sample implementation of such a field in Django:
class CompressedTextField(models.TextField):
def __init__(self, compress_level=6, *args, **kwargs):
self.compress_level = compress_level
super(CompressedTextField, self).__init__(*args, **kwargs)
def to_python(self, value):
value = super(CompressedTextField, self).to_python(value)
return zlib.compress(value.encode(), self.compress_level)
def get_prep_value(self, value):
value = super(CompressedTextField, self).get_prep_value(value)
return zlib.decompress(value).decode()
This field has an extra parameter compared to a regular TextField:
class TagTrend(models.Model):
tag = models.CharField(max_length=1024)
# zlib offers compression levels 0-9
# 0 is no compression
# 9 is maximum compression
trendData = CompressedTextField(compress_level=9)
As an example, storing the string 'a' * 1024 (which is 1024 bytes) when compressed is only 17 bytes.
Do note that the limitation of using such a field is that the data is stored compressed. This means your database queries will search/filter using the compressed version.

How to pass data from django to d3.js

I'm doing a survey web project at the moment that requires me to use d3.js to perform some kind of bar chart visualization. I'm using Django to program my web application.
The problem I encounter is how do I pass all values related to an object into an array that is used in d3.js?
choice = question.choicelist.get(choice_no=choice_no)
where votes for each choices to the question will be choice 1 = 4, choice 2 = 6, choice 3 = 7, choice 4 = 1.
For ds.js, the most simplest way to read a data set is:
var data = [ 4, 6, 7, 1]
How do I pass the data to my template such that d3.js is able to read it as the code above?
Handiest option: convert it to json. Json is valid javascript, so you can insert it directly into the template, which seems to be what you want. Something like
import json
def your_view(request):
poll_results = [4, 6, 7, 1]
poll_as_json = json.dumps(poll_results)
# Gives you a string '[4, 6, 7, 1]'
...
return render_or_whatever(context={'poll_as_json': poll_as_json})
And in your template:
<script ...>
var data = {{ poll_as_json }};
...
</script>
Something like that?

making separate array from existing json django

I have this array of JSON:
[{'pk': 4L, 'model': u'logic.member', 'fields': {'profile': 3L, 'name': u'1', 'title': u'Mr', 'dob': datetime.date(1983, 1, 1), 'lastname': u'jkjk', 'redressno': u'jdsfsfkj', 'gender': u'm'}}, {'pk': 5L, 'model': u'logic.member', 'fields': {'profile': 3L, 'name': u'2', 'title': u'Mr', 'dob': datetime.date(1983, 1, 1), 'lastname': u'jkjk', 'redressno': u'jdsfsfkj', 'gender': u'm'}}]
I want to make separate array of JSON for only fields property.
What I tried is:
memarr=[]
for index,a in data1:
print index
print a
memarr[index]=a[index].fields
And it is giving an error of:
too many values to unpack
Please correct.
First of all, data1 is a list, so you can't unpack it into 2 variables.
If you want the index, you have to use something like enumerate.
Second, you can't assign to a list via indexing if the key doesn't exist. You have to append or use another valid list insert method.
Third, a[index].fields doesn't really make sense - there is no key in a that would be associated with an integer index and fields is not an attribute.
You're probably looking for something like this:
memarr = []
for index, a in enumerate(data1):
memarr.append(a['fields'])
So many things wrong with that snippet...
Anyway:
memarr = [a['fields'] for a in data]