2015-11-12 55 views
1

我發現了以下回溯上具有相反的關係 -<django.db.models.fields.related.RelatedManager在0x7ff4e003d1d0對象>不是JSON序列化

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 

串行

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 

模型

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' 

視圖

class TblserversViewSet(viewsets.ModelViewSet): 
    """ 
    API endpoint that allows users to be viewed or edited. 
    """ 
    queryset = Tblservers.objects.all() 
    serializer_class = TblserversSerializer 
    filter_class = TblserversFilter 

回答

2

您需要執行一個查詢來獲取系列化的數據,當你有關係,例如,你可以做這樣的事情:

my_object.relatedmodel_set # This a RelatedManager 

但是,如果你這樣做:

my_object.relatedmodel_set.all() # This is queryset 
相關問題