I want to create entries in two tables (Log and Post) using the DRF Browseable API POST form.
The example below is contrived, but it outlines what I am trying to do.
From what I can tell you want to create a nested Log object. There are 2 ways of doing this:
POST
Requests, One to create the Post, and the other to create the Log contained the received HTTP 200 data from the API.POST
and parse it server side. Django Rest Framework takes care of this for you.I have changed your code so that it should work. Source
class LogSerializer(serializers.ModelSerializer):
class Meta:
model = Log
fields = ('ip', 'name')
class PostSerializer(serializers.ModelSerializer):
log = LogSerializer()
class Meta:
model = Post
fields = ('info', 'log')
views.py
import generics
class PostCreateAPIView(generics.CreateAPIView):
model = Post
serializer_class = PostSerializer
Then you can send a POST
Request containing 'info', 'ip', and 'name'.