python - Django posting nested JSON inside n number of tables -


i having trouble posting nested fields database. want add lender , borrower different tables in database. loan json post format:

{     "lender": {         "email": "lender@hotmail.com"     },     "borrower": {         "email": "borrower@gmail.com"     },     "amount": 4 } 

this post must add lender lender table , borrower borrower table , loan loan table.

my view:

class lenderloanaddapiview(generics.createapiview):     serializer_class = lenderloan_serializers.addlenderloanserializer     pagination_class = standardresultssetpagination      def get_queryset(self):         lender = self.kwargs['lender']         return loan.objects.filter(lender=lender).order_by('-id') 

my serialiser:

class addlenderloanserializer(serializers.modelserializer):     lender = lenderserializer(many=false)     borrower = borrowerserializer(many=false)      class meta:          model = loan         fields = ('lender','borrower', 'amount',)      def create(self, validated_data):         lender = lender(             email = validated_data['lender']         )         borrower = borrower(             email = validated_data['borrower']         )         lender.save()         borrower.save()          return lender 

i think must implement create method in way. unfortunately don't know how. can please me nested serializer? need go on stuck problem.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -