Django Rest Framework auto increment primary key v

2019-08-06 09:22发布

I'm new to using the DRF so I apologize if this is a trivial question but I've had no luck finding an answer thus far.

I'm using DRF along with Angularjs to create a single page application. When I make posts to my API I get this error to create a new Task object: task_id: [This field is required.] task_id is my primary key on this object. How can I make it so that it gets incremented automatically like it would on a Django Model Form?

class TaskSerializer(serializers.ModelSerializer):         

    class Meta:                                                      
        model = Task                                       
        fields = ('route', 'date', 'task_id', )                    

class AddTask(generics.CreateAPIView):          
    serializer_class = TaskSerializer                                                    

    def get(self, request, format=None):                  
        response = {}                                     

        response['form'] = TaskForm().as_p()    

        return Response(response)  

1条回答
时光不老,我们不散
2楼-- · 2019-08-06 09:52

Are you using task_id in your application page? If not, then remove it from the serializer and DRF will automatically take care of this for you.

Something like this:

class TaskSerializer(serializers.ModelSerializer):         

    class Meta:                                                      
        model = Task                                       
        fields = ('route', 'date',)
查看更多
登录 后发表回答