简体   繁体   English

处理DeliciousPie和Django API的请求数据

[英]Manipulate Request data of TastyPie and Django API

I'm writing an application with django-tastypie and following are my models.py and resource.py files. 我正在用django-tastypie编写应用程序,以下是我的models.py和resource.py文件。

Models.py: Models.py:

import uuid

from django.db import models


class User(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
    name = models.CharField(max_length=50, null=False)
    email = models.EmailField(max_length=254, null=False)
    password = models.CharField(max_length=100, null=False)
    role = models.CharField(max_length=16, default='basic', null=False)

    def __unicode__(self):
        return self.name, self.email

Resources.py: Resources.py:

from tastypie.resources import ModelResource
from tastypie.authorization import Authorization

from api.models import User


class UserResource(ModelResource):
    class Meta:
        queryset = User.objects.all()
        resource_name = 'user'
        authorization = Authorization()
        excludes = ['password']
        #allowed_methods = ['get']

Now the thing is that whenever I hit an API end point from postman, the user is created directly. 现在的问题是,每当我从邮递员到达API终点时,便会直接创建用户。 Now what I don't understand is that whether the request data goes into resources and then into database or directly into the database? 现在我不明白的是,请求数据是进入资源,然后进入数据库还是直接进入数据库? Actually, the thing is that I need to apply some changes to the data before it is stored in the database, like hashing the password and then storing the object in the database. 实际上,我需要对数据进行一些更改,然后再将其存储在数据库中,例如对密码进行哈希处理,然后将对象存储在数据库中。 I'm new to django, so how can I achieve that? 我是django的新手,那么如何实现呢? Like in Flask, we can do something like: 像在Flask中一样,我们可以执行以下操作:

@user.route('/users', methods=['POST'])
def create_user(user_id):

    data = request.get_json(force=True)

    # do all the changes we want

    user = User(data)
    db.session.add(user)
    db.session.commit()

Now if any request comes at '/users' endpoint, we can get it's data in the 'data' variable and then whatever changes we want before storing in the database. 现在,如果有任何请求来自'/ users'端点,我们可以在'data'变量中获取它的数据,然后在存储到数据库中之前进行任何想要的更改。 But how to do that in django with tastypie. 但是,如何在Django中用好吃的东西做到这一点。

Any help would be appreciated 任何帮助,将不胜感激

If you have to massage data before entering into database then Tastypie has the notion of hydrate and dehydrate methods. 如果您在进入database之前必须按摩数据,那么Tastypie具有hydratedehydrate方法的概念。

Check that. 检查一下。 Here is reference hydrate and dehydrate 这是参考水合物和脱水物

In every web framework the data that sent with the request passed to the api endpoint through some mechanism and the same thing happens in Tastypie (you can read about it in Tastypie documentation under Flow Through The Request/Response Cycle ). 在每个Web框架中,与请求一起发送的数据都是通过某种机制传递给api端点的,并且相同的事情发生在Deliciouspie中(您可以在Thastypie文档中的“ 通过请求/响应周期流动”中进行阅读)。

If you want to change the data that you sending/receiving read about Hydrate/Dehydrate , in your case you want to use dehydrate on user password but I recommend you to save the effort and instead use custom user model by inheriting from AbstractUser , that way you can get hashed password by default when User object is saved to your DB. 如果您想更改发送/接收的有关Hydrate / Dehydrate的数据 ,在您的情况下,您想在用户密码上使用dehydrate,但我建议您节省精力,而是通过从AbstractUser继承来使用自定义用户模型,当用户对象保存到数据库时,默认情况下您可以获得哈希密码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM