Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<django.db.models.fields.related.RelatedManager object at 0x7ff4e003d1d0> is not JSON serializable

I'm getting the following traceback on a reverse relationship -

Traceback:
File "/opt/common/external/python_modules/django/core/handlers/base.py" in get_response
  164.                 response = response.render()
File "/opt/common/external/python_modules/django/template/response.py" in render
  158.             self.content = self.rendered_content
File "/opt/common/external/python_modules/rest_framework/response.py" in rendered_content
  71.         ret = renderer.render(self.data, media_type, context)
File "/opt/common/external/python_modules/rest_framework/renderers.py" in render
  674.         context = self.get_context(data, accepted_media_type, renderer_context)
File "/opt/common/external/python_modules/rest_framework/renderers.py" in get_context
  635.             'content': self.get_content(renderer, data, accepted_media_type, renderer_context),
File "/opt/common/external/python_modules/rest_framework/renderers.py" in get_content
  397.         content = renderer.render(data, accepted_media_type, renderer_context)
File "/opt/common/external/python_modules/rest_framework/renderers.py" in render
  103.             separators=separators
File "/opt/common/external/anaconda/2.3.0/lib/python2.7/json/__init__.py" in dumps
  250.         sort_keys=sort_keys, **kw).encode(obj)
File "/opt/common/external/anaconda/2.3.0/lib/python2.7/json/encoder.py" in encode
  209.             chunks = list(chunks)
File "/opt/common/external/anaconda/2.3.0/lib/python2.7/json/encoder.py" in _iterencode
  431.             for chunk in _iterencode_list(o, _current_indent_level):
File "/opt/common/external/anaconda/2.3.0/lib/python2.7/json/encoder.py" in _iterencode_list
  332.                 for chunk in chunks:
File "/opt/common/external/anaconda/2.3.0/lib/python2.7/json/encoder.py" in _iterencode_dict
  408.                 for chunk in chunks:
File "/opt/common/external/anaconda/2.3.0/lib/python2.7/json/encoder.py" in _iterencode
  442.             o = _default(o)
File "/opt/common/external/python_modules/rest_framework/utils/encoders.py" in default
  64.         return super(JSONEncoder, self).default(obj)
File "/opt/common/external/anaconda/2.3.0/lib/python2.7/json/encoder.py" in default
  184.         raise TypeError(repr(o) + " is not JSON serializable")

Exception Type: TypeError at /api/servers/
Exception Value: <django.db.models.fields.related.RelatedManager object at 0x7f5bfe876990> is not JSON serializable

Serializer

class TblserversSerializer(serializers.ModelSerializer):
    bmccode = serializers.SerializerMethodField('get_bmc')

    def get_bmc(self, obj):
        code = Tblbmcgroups.objects.filter(groupid = obj.recordownerid).values_list('bmccode')[0][0]
        return code

    class Meta:
        model = Tblservers
        fields = ('serverid', 'servername', 'isvirtual', 'locationid', 'managedbyid', 'operatingsystem', 'model',
                  'assettag', 'inceptiondate', 'comments', 'assetstatusid', 'recordownerid', 'lastmodifieddate',
                  'lastmodifiedby', 'bmccode', 'server_applicationlinks')
        depth = 1

Models

class Tblvmserverlinks(models.Model):
    id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase.
    virtualserverid = models.IntegerField(db_column='virtualServerId') # Field name made lowercase.
    underlyingserverid = models.IntegerField(db_column='underlyingServerId') # Field name made lowercase.
    comments = models.TextField(blank=True)
    lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True) # Field name made lowercase.
    lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True) # Field name made lowercase.
#    upsize_ts = models.TextField(blank=True) # This field type is a guess.
    class Meta:
        managed = False
        db_table = 'tblVMServerLinks'

class Tblapplicationserverlinks(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True) # Field name made lowercase.
    applicationid = models.ForeignKey('Tblapplications', db_column='applicationId', related_name='application_serverlinks') # Field name made lowercase.
    serverid = models.ForeignKey('Tblservers', db_column='serverId', related_name='server_applicationlinks') # Field name made lowercase.
    environmentid = models.IntegerField(db_column='environmentId') # Field name made lowercase.
    comments = models.TextField(blank=True)
    lastmodifieddate = models.DateTimeField(db_column='lastModifiedDate', blank=True, null=True) # Field name made lowercase.
    lastmodifiedby = models.CharField(db_column='lastModifiedBy', max_length=255, blank=True) # Field name made lowercase.
#    upsize_ts = models.TextField(blank=True) # This field type is a guess.
    class Meta:
        managed = False
        db_table = 'tblApplicationServerLinks'

view

class TblserversViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """
    queryset = Tblservers.objects.all()
    serializer_class = TblserversSerializer
    filter_class = TblserversFilter
like image 442
whoisearth Avatar asked Nov 12 '15 16:11

whoisearth


1 Answers

You need to perform a query to get serializable data, when you have a relationship for example you can do something like this:

my_object.relatedmodel_set  # This a RelatedManager

But if you do:

my_object.relatedmodel_set.all()  # This is queryset
like image 144
Gocht Avatar answered Sep 30 '22 14:09

Gocht