Suppressing SAVE of object in POST - Django Rest F

2019-05-20 03:55发布

问题:

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

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

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

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

回答1:

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