python - 通过返回非预定义字段。 Django 中的 Tastypie API

标签 python django tastypie

我使用 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/

相关文章:

Django Tastypie 和 Django 1.4

mysql - 如何在 Django Tastypie 中使用 OR

Python dictionary.keys() 错误

python - 如何在 pandas date_range 方法中包含结束日期?

python - Django Rest Framework urls 路由器仅识别一个路径

python - 属性错误: 'Tensor' object has no attribute 'compile'

mysql - MySQL utf8mb4 表的 Django "Illegal mix of collations"

python - 如何在 Django 中获取 COUNT 查询

python - Django 表单

python - Tastypie GET ManyTomany 错误