通过以下方式返回非预定义字段.API with Tastypie in Django.



我正在使用Tastypie作为非ORM数据源(Amazon Dynamodb)。我已经浏览了非ORM源代码的官方文档,并找到了以下代码:

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,消息,创建..在这里定义。有什么方法可以返回此处未定义的那些字段,即字典在obj_get_list或obj_get中返回的所有字段。

您可以使用脱水方法。只需将新密钥添加到 bundle.data 即可。

def dehydrate(self, bundle):
    for item in bundle.obj.iteritems():
        bundle.data["new_key"] = "new_value"
    return bundle

最新更新