排除好吃的帖子数据中的某些字段 [英] exclude some fields in tastypie post data

查看:54
本文介绍了排除好吃的帖子数据中的某些字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我通过 django-tastypie 为我的项目启动了一个API.实际上,我想在发布请求中排除某些字段要求.

recently i start an API for my project by django-tastypie. actually I want to exclude some field requirement in post requests.

假设我的模型有四个字段,并且在 django 模型中所有字段均按要求定义.但是我想从API请求中接收其中两个,另外两个将由我的函数填充.

Assume that my model have four fields and all of them defined as require in django model. But I want to receive two of them from API request and 2 others will be filled by my functions.

那么,我如何告诉 tastypie 仅接收这两个字段而跳过其他字段?

So, how could I tell to tastypie to receive just those two fields and skip others?

推荐答案

如果要排除相同的字段,可以通过在资源"的元类中定义它来实现,例如:

If you want to exclude same fields you can do that by define it in the meta class of the Resource, for example:

class MyResource(ModelResource):
     class Meta:
         excludes = (field1, field2)

这些字段每次都会被排除在此资源之外.

And those fields will be excluded every time for this resource.

但是,如果您只想在职位上获得与我一样的方式,那就是通过覆盖脱水方法:

But if you want only on post to get different fields the way how I am doing that is by overriding dehydrate method:

def dehydrate(self, bundle):
     if bundle.request.META['REQUEST_METHOD'] == 'POST':
         bundle.data = dict(my_field1=bundle.obj.my_func1(),
                            my_field2=bundle.obj.my_func2()
                            )
     return bundle

这篇关于排除好吃的帖子数据中的某些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆