Thursday, April 26, 2018

Re: Django - Angular 4 Serializer for inserting Parent and child Models using single JSON post

Hey dear, how to get out of the group conference? plz help

On Mon, Apr 9, 2018 at 3:22 PM, Nathan Sivathanu <nathan.sivathanupillai@gmail.com> wrote:

My requirement - I have two models TravelReq and TravelReqDetails as below

class TravelReq(models.Model):       empid= models.CharField(max_length=7)       startdate= models.DateField()      enddate= models.DateField()      remarks= models.CharField(max_length=30)      missionname= models.CharField(max_length=30)      def __str__(self):          return str(self.id)    class TravelReqDetails(models.Model):       travelReqID = models.ForeignKey(TravelReq ,related_name  = 'req_details' , on_delete=models.CASCADE );       travelDate= models.DateField()       lfrom = models.CharField(max_length=30)       to= models.CharField(max_length=30)       remarks = models.CharField(max_length=30)    def __str__(self):      return str(self.id)

My serailizer

from  .models import TravelReq ,TravelReqDetails  from rest_framework import serializers    class TravelReqSerializer(serializers.ModelSerializer):      class Meta:          model = TravelReq      fields = ('empid' ,     'startdate' ,   'enddate' , 'remarks' , 'missionname' ,'req_details')code here

My view

from .models import TravelReq   from rest_framework import viewsets  from .serializers import TravelReqSerializer       class TravelReqViewSet(viewsets.ModelViewSet):      """      API endpoint that allows users to be viewed or edited.      """      queryset = TravelReq.objects.all()       serializer_class = TravelReqSerializer

From Angular I will be receiving below JSON as POST / PUT request . What is the best way to insert the master(TravelReq) and child records (TravelReqDetails) from a single serializer. please advice. If this is not the best approach ,please suggest a better / standard approach

[      {          "empid": "32432",          "startdate": "2017-11-16",          "enddate": "2018-03-17",          "remarks": "asd",          "missionname": "asd",          "req_details": [              {                  "id": 1,                  "travelDate": "2017-07-18",                  "lfrom": "indiaemarks",                  "to": "pakistan",                  "remarks": "r",                  "travelReqID": <<need to  get from parent travel request>>               },              {                  "id": 2,                  "travelDate": "2018-04-02",                  "lfrom": "pakistan",                  "to": "india",                  "remarks": "rerutn remarks",                  "travelReqID":  <<need to  get from parent travel request>>              }          ]      },

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/816ab639-a82d-451a-9868-58ef057205b8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADrCSzDnAJ65tVvLokAL3do8XEz6BSX94bXj6CTKLk%2BM2A5zfw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment