django从jsonfield的对象列表中获取价值总和
我有一个具有以下实现的模型,
class MyModel(models.Model):
data = models.JSONField(null=True, blank=True)
i具有这种格式的json:
{
"name": "Name",
"irrelevant_list": [],
"items": [
{
"name": "Name 1",
"quantity": 1,
"other_list_of_objects": []
},
{
"name": "Name 2",
"quantity": 2,
"other_list_of_objects": []
}
]
}
我想用dentity
的总和注释DB行(IE Quantity = 3对于此DB行),我尝试了多种方法,仍然没有正确的查询。
I have a model with the following implementation
class MyModel(models.Model):
data = models.JSONField(null=True, blank=True)
The I have json in this format:
{
"name": "Name",
"irrelevant_list": [],
"items": [
{
"name": "Name 1",
"quantity": 1,
"other_list_of_objects": []
},
{
"name": "Name 2",
"quantity": 2,
"other_list_of_objects": []
}
]
}
I want to annotate db rows with the sum of quantity
(i.e. quantity = 3 for this db row), I have tried multiple approaches, still not getting the right query.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加一个名为
数量
charfield的字段,然后覆盖clean()
方法从实例中计算数量。如果您想在Django Admin以外保持一致性,请不要忘记覆盖
save()
方法,以调用full_clean()
方法。这样的东西
You could add a field named
quantities
CharField, and override theclean()
methodto calculate the quantities from the instance. Don't forget to override the
save()
method to call thefull_clean()
method if you wanna keep consistency outside of django admin.Something like this