Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#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)

comment:1 by Arun Kumar, 8 years ago

For json fields you need to do

itemscampaign.filter(data__processed=True)

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

Last edited 8 years ago by Arun Kumar (previous) (diff)

comment:2 by Martín Peveri, 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'>

Version 0, edited 8 years ago by Martín Peveri (next)

in reply to:  2 comment:3 by Arun Kumar, 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 Tim Graham, 8 years ago

Resolution: invalid
Status: newclosed

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 Martín Peveri, 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?

Note: See TracTickets for help on using tickets.
Back to Top