我使用 Tastypie 作为非 ORM 数据源 (Amazon Dynamodb)。我已经通过官方documentation for non-ORM source并找到以下代码:
class MessageResource(Resource):
# Just like a Django ``Form`` or ``Model``, we're defining all the
# fields we're going to handle with the API here.
uuid = fields.CharField(attribute='uuid')
user_uuid = fields.CharField(attribute='user_uuid')
message = fields.CharField(attribute='message')
created = fields.IntegerField(attribute='created')
我是 Tastypie 的新手,我的理解是 API 返回的字段 uuid、message、created.. 是在这里定义的。有什么方法可以返回此处未定义的字段,即 obj_get_list 或 obj_get 中的字典返回的所有字段。
最佳答案
可以使用脱水法。只需向bundle.data 添加一个新 key 即可。
def dehydrate(self, bundle):
for item in bundle.obj.iteritems():
bundle.data["new_key"] = "new_value"
return bundle
关于python - 通过返回非预定义字段。 Django 中的 Tastypie API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19738478/