0

I can't get Django to serialize the AL_NODE as a modelserializer. Is it possible to serialize AL_NODEs?

Here is my code:

class UserSecuritySelectionModelSerializers(serializers.ModelSerializer):

class Meta:
    model = UserSecuritySelectionModel()
    fields = ('hasChildNode', 'classificationNames', 'tgtWeight','currWeight','SSM','ext_model_id')

Here is a sample of the data and how it is structured in the database: Database

Code in my views.py

if request.is_ajax() and id is not None:

    rootNode = UserSecuritySelectionModel.objects.get(SSM_id=id, classificationNameNode__isnull=True)

    if not rootNode.is_root():
        node = rootNode.get_root()
        data = serializers.serialize('json', node, use_natural_foreign_keys=True)
        return JsonResponse(data, safe=False)

userSelectionModelSerializer = UserSecuritySelectionModelSerializers(rootNode)
#data = serializers.serialize('json', [rootNode], use_natural_foreign_keys=True)

return JsonResponse (userSelectionModelSerializer.data, status=201, safe=False)
Axwack
  • 569
  • 1
  • 8
  • 26

1 Answers1

0

You say it's not working but you haven't included any details about the error you are getting. This makes everything here a guess.

You should just be setting the model reference to a class, not actually creating an instance of the model

model = UserSecuritySelectionModel()

# should be
class Meta:
    model = UserSecuritySelectionModel

Next, I think you should explicitly pass in the instance just to be safe. It's not required, but it makes your intention clear:

UserSecuritySelectionModelSerializers(instance=rootNode)

Third, just return a standard Response, not a JsonResponse, DRF will do the content negotiation for you. That is why you are using it.

return Response(MyLongSerializer(instance=root).data)

Finally, is there any reason for sending a 201? You do not appear to be creating anything in the view. If you are, then send it like this:

return Response(..., status=HTTP_201_CREATED)
Andrew
  • 8,322
  • 2
  • 47
  • 70