#28012 closed Uncategorized (invalid)
JsonField PostgreSQL problem
Reported by: | Martín Peveri | Owned by: | |
---|---|---|---|
Component: | contrib.postgres | Version: | 1.10 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi, I have a model with a field JsonField of PostgreSQL of name 'data', like this:
class ItemCampaign(models.Model): campaign = models.ForeignKey( Campaign, related_name="itemscampaign", verbose_name="Item campaña" ) data = JSONField(default=dict) def __str__(self): return self.campaign.name
In this field i have one record with this data, for example:
[{'number': '1160188479', 'id': 0, 'content': 'hello', 'processed': True}, {'number': '1160188479', 'id': 1, 'content': 'hello', 'processed': False}, {'number': '1160188479', 'id': 2, 'content': 'hello', 'processed': False}, {'number': '1162341721', 'id': 3, 'content': 'hello', 'processed': False}, {'number': '1162341721', 'id': 4, 'content': 'hello', 'processed': False}, {'number': '1162341721', 'id': 5, 'content': 'hello', 'processed': False}]
If i want filtered, like this:
itemscampaign.filter(data__contains=[{'processed': True}])
This code returned all content data field, Instead of just the dict with 'processed' in True. I want know if this is an error or not is possible.
Thanks!
Change History (5)
follow-up: 3 comment:2 by , 8 years ago
Hi, That does not give me back anything.
The content type of data is:
>>> type(c.itemscampaign.all()[0].data) <class 'list'>
comment:3 by , 8 years ago
The above statement has to return 'dict'.
Replying to Martín Peveri:
Hi, That does not give me back anything.
The content type of data is:
>>> type(c.itemscampaign.all()[0].data) <class 'list'>
comment:4 by , 8 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
There might be a way to use QuerySet.annotate()
to add an attribute with the result you expect, however, please see TicketClosingReasons/UseSupportChannels for places to usage questions like this.
comment:5 by , 8 years ago
Ok, perfect. And how can I insert many dictionaries in the field, as I have in my data if it is not in a list?
For json fields you need to do
Your current query is only checking for existing of processed key in data.