如何通过Django-MongoDB中的JSON字段数据进行分组
我使用的是Django 3.0.5,Python 3.8.0和MongoDB 5.0
我的要求:
通过匹配宽度,高度,厚度和类型字段的值来将数据分组。在分组数据时,进行长度的求和。
我尝试过这样的方法:
list(UploadCableTrayData.objects.filter(creator_id__in=selected_users).filter(date_added__range=(selected_start_date, selected_end_date)).values(
'Thickness__value', 'Width__value', 'Height__value', 'Selection').annotate(dcount=Count(['Thickness', 'Width', 'Height']), Length=Sum('Length__value')).order_by("date_updated"))
,但是我遇到了这样的错误
nameError:名称'厚度__ value'未定义
这是我的模型:
class UploadCableTrayData(BaseModel, models.Model):
"""
Model to store Cable Tray data
"""
order_upload_id = models.AutoField(primary_key=True)
Order_number = jsonfield.JSONField(null=True, blank=True)
Type = jsonfield.JSONField(null=True, blank=True)
Selection = jsonfield.JSONField(null=True, blank=True)
Height = jsonfield.JSONField(null=True, blank=True)
Width = jsonfield.JSONField(null=True)
Box_width = jsonfield.JSONField(null=True)
Box_height = jsonfield.JSONField(null=True)
Length = jsonfield.JSONField(null=True)
Inner_bend1 = jsonfield.JSONField(null=True, blank=True)
Inner_bend2 = jsonfield.JSONField(null=True, blank=True)
Thickness = jsonfield.JSONField(null=True, blank=True)
Rung_width = jsonfield.JSONField(null=True, blank=True)
Rung_height = jsonfield.JSONField(null=True, blank=True)
Distance_between_rungs = jsonfield.JSONField(null=True, blank=True)
project = models.ForeignKey('project.Project', on_delete=models.DO_NOTHING)
def __str__(self):
return str(self.order_upload_id)
class Meta:
db_table = 'UploadCableTrayData'
模型中存储的数据就是这样:
{
"_id": {
"$oid": "62384f772d6545101709083b"
},
"order_upload_id": 4,
"creator_id": 3,
"updater_id": 3,
"date_added": {
"$date": {
"$numberLong": "1647857527370"
}
},
"date_updated": {
"$date": {
"$numberLong": "1647857527370"
}
},
"is_deleted": false,
"Order_number": {
"key": "order",
"value": "o1"
},
"Type": {
"key": "type",
"value": "pct"
},
"Selection": [{
"key": "material",
"value": "m1"
}],
"Height": {
"key": "height",
"value": "200"
},
"Width": {
"key": "width",
"value": "200"
},
"Box_width": {
"key": "box_width",
"value": ""
},
"Box_height": {
"key": "box_height",
"value": ""
},
"Length": {
"key": "running_meter",
"value": "200"
},
"Inner_bend1": {
"key": "inner_bend1",
"value": ""
},
"Inner_bend2": {
"key": "inner_bend2",
"value": ""
},
"Thickness": {
"key": "thickness",
"value": "1"
},
"Rung_width": {
"key": "rung_width",
"value": ""
},
"Rung_height": {
"key": "rung_height",
"value": ""
},
"Distance_between_rungs": {
"key": "distance_between_rungs",
"value": ""
},
"project_id": 2
}
I am using Django 3.0.5, Python 3.8.0, and MongoDB 5.0
My requirement:
Group the data by matching the value of Width, Height, Thickness, and Type fields. While grouping the data, do the summation of Length.
I tried this way:
list(UploadCableTrayData.objects.filter(creator_id__in=selected_users).filter(date_added__range=(selected_start_date, selected_end_date)).values(
'Thickness__value', 'Width__value', 'Height__value', 'Selection').annotate(dcount=Count(['Thickness', 'Width', 'Height']), Length=Sum('Length__value')).order_by("date_updated"))
But I am getting errors like this
NameError: name 'Thickness__value' is not defined
Here is my model:
class UploadCableTrayData(BaseModel, models.Model):
"""
Model to store Cable Tray data
"""
order_upload_id = models.AutoField(primary_key=True)
Order_number = jsonfield.JSONField(null=True, blank=True)
Type = jsonfield.JSONField(null=True, blank=True)
Selection = jsonfield.JSONField(null=True, blank=True)
Height = jsonfield.JSONField(null=True, blank=True)
Width = jsonfield.JSONField(null=True)
Box_width = jsonfield.JSONField(null=True)
Box_height = jsonfield.JSONField(null=True)
Length = jsonfield.JSONField(null=True)
Inner_bend1 = jsonfield.JSONField(null=True, blank=True)
Inner_bend2 = jsonfield.JSONField(null=True, blank=True)
Thickness = jsonfield.JSONField(null=True, blank=True)
Rung_width = jsonfield.JSONField(null=True, blank=True)
Rung_height = jsonfield.JSONField(null=True, blank=True)
Distance_between_rungs = jsonfield.JSONField(null=True, blank=True)
project = models.ForeignKey('project.Project', on_delete=models.DO_NOTHING)
def __str__(self):
return str(self.order_upload_id)
class Meta:
db_table = 'UploadCableTrayData'
Data stored in the model is like this:
{
"_id": {
"$oid": "62384f772d6545101709083b"
},
"order_upload_id": 4,
"creator_id": 3,
"updater_id": 3,
"date_added": {
"$date": {
"$numberLong": "1647857527370"
}
},
"date_updated": {
"$date": {
"$numberLong": "1647857527370"
}
},
"is_deleted": false,
"Order_number": {
"key": "order",
"value": "o1"
},
"Type": {
"key": "type",
"value": "pct"
},
"Selection": [{
"key": "material",
"value": "m1"
}],
"Height": {
"key": "height",
"value": "200"
},
"Width": {
"key": "width",
"value": "200"
},
"Box_width": {
"key": "box_width",
"value": ""
},
"Box_height": {
"key": "box_height",
"value": ""
},
"Length": {
"key": "running_meter",
"value": "200"
},
"Inner_bend1": {
"key": "inner_bend1",
"value": ""
},
"Inner_bend2": {
"key": "inner_bend2",
"value": ""
},
"Thickness": {
"key": "thickness",
"value": "1"
},
"Rung_width": {
"key": "rung_width",
"value": ""
},
"Rung_height": {
"key": "rung_height",
"value": ""
},
"Distance_between_rungs": {
"key": "distance_between_rungs",
"value": ""
},
"project_id": 2
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论