Djongo (mongoDb) - Django rest framework Problem with search on JsonFields - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Djongo (mongoDb) - Django rest framework Problem with search on JsonFields (/thread-33624.html) |
Djongo (mongoDb) - Django rest framework Problem with search on JsonFields - grzybekrafal - May-11-2021 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 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:{ |