在POST - Django Rest框架中抑制对象的保存 [英] Suppressing SAVE of object in POST - Django Rest Framework

查看:113
本文介绍了在POST - Django Rest框架中抑制对象的保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与以下问题有关:在Django REST框架Serializer中GET和POST的Assymetric性质。我把它作为一个新鲜的问题,而不是在这个线程中提出更多的问题,相应地根据SO指南

This is related to the question : Assymetric nature of GET and POST in a Django REST framework Serializer . I've put it as a fresh question, instead of putting more questions in that thread, accordingly to SO guidelines

我正在为用户模型编写一个Viewset和一个ModelSerializer提供/用户终端

I am writing a Viewset and a ModelSerializer for the User model to provide a /user endpoint

GET - 以标准DRF方式返回所有用户的列表和信息

GET - returns list and information about all users, in the standard DRF way

POST - 所有我想要的客户端发布的是Facebook access_token(因此将所有其他字段放在read_only中SerialSet中的pre_save()被连线以使用此访问令牌,并使用 django-facebook 从facebook api(使用访问令牌)中提取数据,并自动创建一个新的由于这个新的 用户 是自动创建的,所以我想在POST过程中抑制正常的DRF流程,而不是通过DRF创建另一个用户。我如何做?

POST - all I want the client to post is the facebook access_token (hence have put all other fields as read_only in serializer. The pre_save() in ViewSet is wired to use this access token and it uses django-facebook to pull data from facebook api (using access token) and automatically create a new user with that information. Since this new user is created automatically, I want to suppress the normal DRF flow during POST and not create another user via DRF. How do i do this?

views.py

from open_facebook import OpenFacebook
from django_facebook.api import FacebookUserConverter
from django_facebook.connect import connect_user

class UserViewSet(viewsets.ModelViewSet):

    queryset = models.User.objects.all()

    serializer_class = UserSerializer

    def pre_save(self, obj):


        access_token = obj.access_token
        facebook = OpenFacebook(access_token)
        conv = FacebookUserConverter(facebook)

        action, user = connect_user(self.request, access_token)
        # this creates an entire new row, just as required, in the variable "user", so all I want to do is suppress any other row creation in the standard POST method. connect_user fills in data like first_name, last_name, etc from facebook already, and that is exactly what I need to do.


        conv.get_and_store_friends(user)

        obj = user
        user.delete()
        # I am trying to do that by copying user to obj and deleting user, but at the end of it i 

        print obj.username

serializers.py

class UserSerializer(serializers.HyperlinkedModelSerializer):
    """
User Serializer
"""

    class Meta:
        model = models.User
        fields = ('id', 'username', 'first_name', 'last_name', 'activities', 'image_url', 'url', 'access_token')

        read_only_fields = ('username', 'first_name', 'last_name', 'image_url', 'activities') #todo: find out a shortcut to invert selection

        # show activities with user details rather than separately to remove an extra server call
        depth = 1


推荐答案

使用ModelViewSet的create()函数工作,而不是pre_save来禁止保存对象

using the create() function of ModelViewSet worked, instead of pre_save - to suppress saving the object

这篇关于在POST - Django Rest框架中抑制对象的保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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