- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在反向关系上得到以下回溯 -
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
最佳答案
您需要执行查询以获取可序列化数据,例如,当您有关系时,您可以执行以下操作:
my_object.relatedmodel_set # This a RelatedManager
但如果你这样做:
my_object.relatedmodel_set.all() # This is queryset
关于python - <django.db.models.fields.related.RelatedManager object at 0x7ff4e003d1d0> 不是 JSON 可序列化的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33676289/
感谢您的宝贵时间。我正在尝试获取foreignKey图像模型的第一张图像并与其他字段一起显示 我已经阅读了一些有关相关模型的问题和文档,我得到的最好的结果是在我的模型上创建一个函数,以便在获取第一张图
我有一个这样定义的模型: class UserDetail(models.Model): user = models.ForeignKey(User, db_index=True, uniqu
假设我有两个 models :Book和 Author . 如果我现在添加一个 ForeignKey field至 Book将其连接到 Author ,并实例化一个 Book object - 那么我
这是models.py代码 class Profile(models.Model): user = models.ForeignKey(User, unique=True, on_delete
这是models.py代码 class Profile(models.Model): user = models.ForeignKey(User, unique=True, on_delete
我有一个看起来像这样的模型 class Connections(models.Model): following = models.ForeignKey( User, rela
嘿,我在 SO 上浏览了一些类似的帖子,但没有找到任何解决我问题的方法。我有以下型号, from django.db import models class Areas(models.Model):
我有两个模型:Play 和 PlayParticipant,(部分)定义为: class PlayParticipant(models.Model): player = models.Fore
原来的错误是: Got AttributeError when attempting to get a value for field `original` on serializer `Produc
假设我有一组 Django 模型: class Article(models.Model): title = models.CharField(max_length=100, default=
这个问题是关于RelatedManager.add()的用于将模型对象实例添加到多对一或多对多关系中。为了说明我的问题,假设我有以下两个模型: class Book(models.Model):
我想在我的管理器方法 baz 中访问 Foo 实例 foo: foo.bar_set.baz() baz 通常采用 Foo 类型的参数: BarManager(models.Manager):
在 Django 中,我使用 @receiver 来保存一个名为 CampaignProfile 的模型对象,如下所示: @receiver(post_save, sender=UserModel)
在我的代码中的某处说 publisher.publisherperson_set.remove(email__in=pp_remove_set) 执行此操作后,我得到了 AttributeError:
这个问题与this question直接相关,但那个现在似乎已经过时了。 我正在尝试测试 View 而无需访问数据库。为此,我需要模拟一个 RelatedManager在用户上。 我正在使用 pyte
这是我的(简化的)models.py: class MyList(models.Model): title = models.CharField(max_length=40) part
我有一个 2 层深的嵌套序列化器堆栈: class ToolSerialier(serializers.ModelSerializer): input = ToolInputSerialize
我有一个 Django 模型的客户经理,它覆盖了 create 方法以保存一些相关对象: class CustomManager(models.Manager): def create(sel
如果我有以下型号: class Fubar(models.Model): name = models.CharField() class Related(models.Model):
Django 的 ForeignRelatedObjectsDescriptor.create_manager(...) 函数动态创建 RelatedManager 类并随后初始化动态创建的类的实例。
我是一名优秀的程序员,十分优秀!