This is my first post on dev.to and I think about it more as a note for myself and perhaps someone finds this as interesting as I do.
Wha...
For further actions, you may consider blocking this person and/or reporting abuse
I spend couple hours trying to figure the right ORM Algorithms but I find your article so clean however I followed step by step my code doesn't work the way I expected it to work, the COUNT return result and SUM return None type it does seems like it doesn't getting a value of JSON keys can you please assist ? and what version of Django are you using for this article ?
I am using Django in its most recent version which is 2.2.5.
Can you describe your data structure in more detail in order for me to understand your problem better?
Perhaps you could also provide some example json data?
Here are my code and I used Django 2.2.2, when I query data JSON value does come as None type and the count does bring value
query
my response
and my object looks like this when I query it with normal DataPoint.objects.all()
Unfortunately, all I can say is, it works for me.
Here is my example code that I used to create some example data:
And just for the records, my version, taken from the same shell:
The only thing I changed in your code, is that I removed the
plant_idForeignKeyI am using Django==3.0.5 with mongodb. There is one model UploadCableTrayData and I want consolidated data from it.
For example:
I have multiple entries stored in db like below.
{
_id: ObjectId('61ae186196f098bb40131d29'),
order_upload_id: 1,
Type: '{"key": "type", "value": "pct"}',
Length: '{"key": "length", "value": "1"}',
project_id: 1
}
{
_id: ObjectId('61aed76746ad80bbef86213b'),
order_upload_id: 2,
Type: '{"key": "type", "value": "pct"}',
Length: '{"key": "length", "value": "120"}',
project_id: 2
}
{
_id: ObjectId('61ae186196f098bb40131d29'),
order_upload_id: 1,
Type: '{"key": "type", "value": "lct"}',
Length: '{"key": "length", "value": "11"}',
project_id: 1
}
{
_id: ObjectId('61aed76746ad80bbef86213b'),
order_upload_id: 2,
Type: '{"key": "type", "value": "bct"}',
Length: '{"key": "length", "value": "120"}',
project_id: 2
}
What I want is, summation of Length-->value, by grouping the data of same Type
Final result should look like this
[
{Type: "pct", Length: 121},
{Type: "bct", Length: 120},
{Type: "lct", Length: 11}
]
I tried multiple solutions but none worked for me.
1.
UploadCableTrayData.objects.annotate(length_value_total=Sum(Cast(KeyTextTransform('value', 'Length'),FloatField()))).values('length_value_total').filter(creator_id_in=selected_users).filter(date_added_range=(selected_start_date, selected_end_date))
2.
UploadCableTrayData.objects.annotate(val=KeyTextTransform('value', 'value__Length')).aggregate(Sum('val'))
3.
UploadCableTrayData.objects.annotate(s=RawSQL("((Length->>'value')::int)",(0,))).aggregate(sold=Sum('s'))
Model:
class UploadCableTrayData(BaseModel, models.Model):
"""
Model to store Cable Tray data
After doing the same as mentioned in this article, I am getting below in response.
views.py
data123 = UploadCableTrayData.objects.counts_only()
models.py
class UploadCableTrayDataManager(models.Manager):
def counts_only(self):
return (UploadCableTrayData.objects.annotate(thickness=Cast(KeyTextTransform("value", "Thickness"), models.FloatField())).values("Thickness", "Selection").annotate(thickness_count=Count("Thickness"), running=Sum(Cast(KeyTextTransform("value", "Length"), models.FloatField()))).order_by())
class UploadCableTrayData(BaseModel, models.Model):
"""
Model to store Cable Tray data
Facing a similar kind of problem in my office project. Just going through your code and implemented it and everything work's fine. Thanks for sharing !!!
I love JSONFields. There are so many things that you can do with them (ArrayFields, also) which are somewhat counter intuitive compared to "classical rdbms thinking" but make some problems so much easier.