{{{
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.
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>
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>
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>
* 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>
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>