[Django] #28012: JsonField PostgreSQL problem

3 views
Skip to first unread message

Django

unread,
Apr 2, 2017, 11:36:15 PM4/2/17
to django-...@googlegroups.com
#28012: JsonField PostgreSQL problem
--------------------------------------------+------------------------
Reporter: Martín Peveri | Owner: (none)
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
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!

--
Ticket URL: <https://code.djangoproject.com/ticket/28012>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 3, 2017, 7:14:51 AM4/3/17
to django-...@googlegroups.com
#28012: JsonField PostgreSQL problem
----------------------------------+--------------------------------------

Reporter: Martín Peveri | Owner: (none)
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Arun Kumar):

For json fields you need to do
{{{
itemscampaign.filter(data__contains__processed=True)
}}}

Your current query is only checking for existing of processed key in data.

--
Ticket URL: <https://code.djangoproject.com/ticket/28012#comment:1>

Django

unread,
Apr 3, 2017, 8:35:13 AM4/3/17
to django-...@googlegroups.com
#28012: JsonField PostgreSQL problem
----------------------------------+--------------------------------------
Reporter: Martín Peveri | Owner: (none)
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by 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'>

--
Ticket URL: <https://code.djangoproject.com/ticket/28012#comment:2>

Django

unread,
Apr 3, 2017, 8:46:42 AM4/3/17
to django-...@googlegroups.com
#28012: JsonField PostgreSQL problem
----------------------------------+--------------------------------------
Reporter: Martín Peveri | Owner: (none)
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 1.10
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Arun Kumar):

The above statement has to return 'dict'.

Replying to [comment:2 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'>
> }}}
>
>
>
>
>
>

--
Ticket URL: <https://code.djangoproject.com/ticket/28012#comment:3>

Django

unread,
Apr 3, 2017, 8:53:31 AM4/3/17
to django-...@googlegroups.com
#28012: JsonField PostgreSQL problem
----------------------------------+--------------------------------------
Reporter: Martín Peveri | Owner: (none)
Type: Uncategorized | Status: closed
Component: contrib.postgres | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/28012#comment:4>

Django

unread,
Apr 3, 2017, 9:00:21 AM4/3/17
to django-...@googlegroups.com
#28012: JsonField PostgreSQL problem
----------------------------------+--------------------------------------
Reporter: Martín Peveri | Owner: (none)
Type: Uncategorized | Status: closed
Component: contrib.postgres | Version: 1.10
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------+--------------------------------------

Comment (by Martín Peveri):

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?

--
Ticket URL: <https://code.djangoproject.com/ticket/28012#comment:5>

Reply all
Reply to author
Forward
0 new messages