Python 如何使用DJANGO-tastype在resources.py中编辑帖子
我试图根据帖子中的数据计算django模型中的一个字段。例如,my Person模型包含诸如年龄、身高、体重、性别和BMI等字段,myPython 如何使用DJANGO-tastype在resources.py中编辑帖子,python,django,api,post,tastypie,Python,Django,Api,Post,Tastypie,我试图根据帖子中的数据计算django模型中的一个字段。例如,my Person模型包含诸如年龄、身高、体重、性别和BMI等字段,my视图。py有一个基于其他字段计算BMI的函数 问题 我如何在resources.py中计算BMI,就像在视图中计算BMI一样,并将BMI值添加到POST对象中 例如,在这篇文章中,创建一个具有以下特征的人:年龄、身高、体重、性别,以及后端计算的BMI 通常,这篇文章会创建一个BMI为空的人 curl --dump-header - -H "Content-Type
视图。py
有一个基于其他字段计算BMI的函数
问题
我如何在resources.py中计算BMI,就像在视图中计算BMI一样,并将BMI值添加到POST对象中
例如,在这篇文章中,创建一个具有以下特征的人:年龄、身高、体重、性别,以及后端计算的BMI
通常,这篇文章会创建一个BMI为空的人
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"age": "45", "height": "60, "weight": "130", "gender": "Female"}' http://localhost:8000/api/v1/person/
注意
这只是一个类似于我实际代码的示例。在我的例子中,用户不能像BMI那样轻松地计算字段。也许您要找的是编辑ModelResource并在发布后返回BMI值。请尝试创建如下资源:
from tastypie.resources import ModelResource
from tastypie.utils import trailing_slash
class PersonResource(ModelResource):
class Meta:
resource_name = 'lol'
allowed_methods = ['post'] # note that this resource just accept posts
def base_urls(self):
return [
url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('bmi_api_function'), name="api_lol_api_function"),
]
def bmi_api_function(self, request, **kwargs):
deserialized = self.deserialize(
request, request.body,
format=request.META.get('CONTENT_TYPE', 'application/json')
)
age = deserialized['age'] # all your post data will be at deserialized variable
# you function
bmi = 2 * age
return self.create_response(request, {'bmi': bmi})
从tastypie.resources导入ModelResource
从tastypie.utils导入尾部斜杠
类PersonResource(模型资源):
类元:
资源名称='lol'
允许的_方法=['post']#请注意,此资源只接受post
def基本URL(自身):
返回[
url(r“^(?P%s)%s$”%(self.\u meta.resource\u name,trailing\u slash()),self.wrap\u视图('bmi\u api\u函数'),name=“api\u lol\u api\u函数”),
]
def bmi_api_函数(self、request、**kwargs):
反序列化=自。反序列化(
请求,请求,请求,
format=request.META.get('CONTENT\u TYPE','application/json')
)
age=反序列化['age']#您的所有帖子数据都将位于反序列化变量处
#你的功能
体重指数=2*年龄
返回self.create_响应(请求,{'bmi':bmi})
希望它对您有用也许您正在寻找的是编辑您的ModelResource并在发布后返回BMI值。请尝试创建如下资源:
from tastypie.resources import ModelResource
from tastypie.utils import trailing_slash
class PersonResource(ModelResource):
class Meta:
resource_name = 'lol'
allowed_methods = ['post'] # note that this resource just accept posts
def base_urls(self):
return [
url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('bmi_api_function'), name="api_lol_api_function"),
]
def bmi_api_function(self, request, **kwargs):
deserialized = self.deserialize(
request, request.body,
format=request.META.get('CONTENT_TYPE', 'application/json')
)
age = deserialized['age'] # all your post data will be at deserialized variable
# you function
bmi = 2 * age
return self.create_response(request, {'bmi': bmi})
从tastypie.resources导入ModelResource
从tastypie.utils导入尾部斜杠
类PersonResource(模型资源):
类元:
资源名称='lol'
允许的_方法=['post']#请注意,此资源只接受post
def基本URL(自身):
返回[
url(r“^(?P%s)%s$”%(self.\u meta.resource\u name,trailing\u slash()),self.wrap\u视图('bmi\u api\u函数'),name=“api\u lol\u api\u函数”),
]
def bmi_api_函数(self、request、**kwargs):
反序列化=自。反序列化(
请求,请求,请求,
format=request.META.get('CONTENT\u TYPE','application/json')
)
age=反序列化['age']#您的所有帖子数据都将位于反序列化变量处
#你的功能
体重指数=2*年龄
返回self.create_响应(请求,{'bmi':bmi})
希望对你有用