Python Forum
Djongo (mongoDb) - Django rest framework Problem with search on JsonFields
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Djongo (mongoDb) - Django rest framework Problem with search on JsonFields
#1
Bug 
I have a problem with filtering on JsonFIleds in Django REST API on MongoDB. I use djongo to connect with MongoDB.

Every time I get this error:

    raise FieldError(
    django.core.exceptions.FieldError: 
        Unsupported lookup 'seq' for JSONField or join on the field not permitted.
    [11/May/2021 10:21:00] "GET /rest/broker/topic/?decode_message_seq=34 HTTP/1.1" 500 21577
I would like to search for information in saved json in the database. In the code below it tries to search for data where the decode_message field contains the seq field. I searched the forums and documentation and couldn't find an answer to my bug.

Model:

from django.db import models
from djongo.models import JSONField, DjongoManager


class Topic(models.Model):
    topic = models.TextField(max_length=250)
    message = JSONField()
    decode_message = JSONField(blank=True, null=True)
    create_at = models.DateTimeField(auto_now_add=True, null=True)
    objects = DjongoManager()
Serializer:

from django.db import models
from djongo.models import JSONField, DjongoManager


class Topic(models.Model):
    topic = models.TextField(max_length=250)
    message = JSONField()
    decode_message = JSONField(blank=True, null=True)
    create_at = models.DateTimeField(auto_now_add=True, null=True)
    objects = DjongoManager()
Serializer:

from .models import Topic
from .utilis import decodeMqttMessage
from rest_framework import serializers

class TopicSerializer(serializers.HyperlinkedModelSerializer):
    message = serializers.JSONField()
    decode_message = serializers.JSONField()

class Meta:
    model = Topic
    fields = ['id', 'topic', 'message', 'decode_message', 'create_at']


    def create(self, dict, *args, **kwargs):
        topic = dict.get('topic', None)
        message = dict.get('message', None)
        decode_message = decodeMqttMessage(message)

        new_broker_info = Topic.objects.create(topic=topic, message=message, decode_message=decode_message)
        new_broker_info.save()

        return new_broker_info
Filters:

import django_filters
from .models import Topic

class TopicFilter(django_filters.rest_framework.FilterSet):
    decode_message_seq = django_filters.CharFilter(field_name="decode_message__seq", lookup_expr='icontains')

    class Meta:
        model = Topic
        fields = ["id", "topic", "decode_message_seq", "create_at"]
Saved record in database:

Quote:{
"_id" : ObjectId("60890486a92950cd4f4b9098"),
"id" : NumberInt(1),
"topic" : "spBv1.0/9991/DDATA/9991/P7_HMI2",
"message" : {
"metrics" : [
{
"dataType" : "UInt16",
"name" : "CPUusage",
"timestamp" : NumberLong(1618999291304),
"value" : NumberInt(74)
}
],
"seq" : NumberInt(135),
"timestamp" : NumberLong(1618999291304)
},
"decode_message" : {
"seq" : NumberInt(135),
"timestamp" : NumberLong(1618999291304),
"CPUusage" : NumberInt(74)
},
"create_at" : ISODate("2021-04-28T06:45:26.854+0000")
}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Django authenticate problem Carolzi 0 629 Oct-24-2022, 07:10 AM
Last Post: Carolzi
  swapping data in mongodb saisankalpj 0 708 Aug-30-2022, 12:42 PM
Last Post: saisankalpj
  Django: Adding Row Data To Existing Model Instance Question/Problem. Steven_Pinkerton 1 1,214 Aug-09-2022, 10:46 AM
Last Post: Addweb
Exclamation MongoDB cannot connect - pymongo speedev 1 2,055 Aug-21-2021, 01:35 PM
Last Post: ndc85430
  problem python search engine forumpy 0 1,701 Sep-15-2020, 10:26 PM
Last Post: forumpy
  Retrieve data from MongoDB dsuk 2 2,261 Aug-24-2020, 09:22 PM
Last Post: micseydel
  find a string in a field in MongoDB Leon79 2 2,371 Jul-19-2020, 09:20 PM
Last Post: menator01
  insert more data at once in MongoDB Leon79 2 2,288 Jul-05-2020, 01:34 PM
Last Post: Leon79
  instaloaders problem: search certain hashtag - posts without hashtag extracted ledgreve 0 2,122 Nov-18-2019, 01:22 PM
Last Post: ledgreve
  Load JSON file data into mongodb using pymongo klllmmm 1 11,799 Jun-28-2019, 12:47 AM
Last Post: klllmmm

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020