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:
Model:
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 21577I 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_infoFilters:
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")
}