在Django ORM中,如何为JSONField中的值get_or_create?

时间:2017-07-05 12:06:44

标签: python django django-models django-queryset django-jsonfield

我正在使用像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)

我尝试过上面的操作,但它只是将字段设置为空字典。

1 个答案:

答案 0 :(得分:0)

试试这个: -

models.py :-
class Subscription(models.Model):
    data = JSONField(db_index=True,null=True)

views.py:-
DictToStore = {"product_id": 123, "available": False}
subscription, new = Subscription.objects.get_or_create(data=DictToStore)