close
The Wayback Machine - https://web.archive.org/web/20200921021412/https://github.com/nesdis/djongo/issues/457
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nested document is not serialized in json format #457

Open
RamazanDemirci opened this issue Aug 17, 2020 · 0 comments
Open

nested document is not serialized in json format #457

RamazanDemirci opened this issue Aug 17, 2020 · 0 comments

Comments

@RamazanDemirci
Copy link

@RamazanDemirci RamazanDemirci commented Aug 17, 2020

i try store nested json document in mongodb. insert is ok but when i get document nested object not json format but string

i want to store json files in mogodb and create and get with django rest framework.

i share my code below. this code insert posted data into mongo.
but when i 'get' data, nested object represent as string. i want to get object as json.

i reseached a lot but i haven't found anything about this.

postman get result :

{
"id": 1,
"country": "United Kingdom",
"country_code": "UK",
"nested": "{"name": "London", "icon": "icon_url"}" #problem here. this field must be json object. in mogodb json object here
}

Python script

###########################models.py########################

from djongo import models

class Nest(models.Model):
    name = models.CharField(max_length=150, blank=True, null=True)
    icon = models.CharField(max_length=150, blank=True, null=True)

    class Meta:
        abstract = True

class Root(models.Model):
    #_id = models.ObjectIdField()
    country = models.CharField(max_length=200, blank=True, null=True)
    country_code = models.CharField(max_length=5, blank=True, null=True)
    nested = models.EmbeddedField(
        model_container=Nest, null=True)
		
###########################serializers.py#####################
from rest_framework import serializers
from .models import Root

class RootSerializer(serializers.ModelSerializer):

    class Meta:
        model = Root
        fields = '__all__'
		
###########################views.py###########################
from django.shortcuts import render

from django.http.response import JsonResponse
from rest_framework.parsers import JSONParser
from rest_framework import status

from .models Root
from .serializers import RootSerializer
from rest_framework.decorators import api_view

@api_view(['GET', 'POST', 'DELETE'])
def root(request):
    if request.method == 'GET':
        first_root = Root.objects.first()
        root_serializer = RootSerializer(first_root, many=False)
        return JsonResponse(root_serializer.data, safe=False)
    elif request.method == 'POST':
        root_data = JSONParser().parse(request)
        root_serializer = RootSerializer(data=root_data)
        if root_serializer.is_valid():
            root_serializer.save()
            return JsonResponse(root_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(root_serializer.errors, status=status.HTTP_400_BAD_REQUEST)

###########################urls.py###########################
from django.conf.urls import url
from rootApp import views

urlpatterns = [
    url(r'^api/root$', views.root),
]

###########################settings.py#######################
DATABASES = {
    'default': {
        'ENGINE': 'djongo',
        'NAME': 'root_db',
        'HOST': '127.0.0.1',
        'PORT': 27017,
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.