【发布时间】:2017-07-05 12:06:44
【问题描述】:
我有一个 Django 模型,例如:
class Subscription(models.Model):
data = JSONField(default=dict)
我想做这样的事情:
data = {"product_id": 123, "available": False}
subscription, new = Subscription.objects.get_or_create(data__product_id=123,
data__available=False)
我尝试执行上述操作,但它只是将字段设置为空字典。
【问题讨论】:
-
试试这个订阅,new = Subscription.objects.get_or_create(product_id=data['product_id'], available=data['available'])
-
为什么你使用 __ 两个下划线而不是一个?
-
我相信你正在寻找这个
Subscription.objects.get_or_create(data=data) -
只需将
data=data放入get_or_create() -
Subscription.objects.get_or_create(data={'product_id': 123, available=False})
标签: python django django-models django-queryset django-jsonfield