Insert data into multiple models when doing POST method

335 views
Skip to first unread message

thiagarajan.kpt

unread,
May 21, 2018, 3:18:50 PM5/21/18
to Django users
Hi,

I have two models (Patients and PatientAddress). I want to create a new patient using POST method and data should be inserted in Patient and PatientAddress tables. But the data is inserted only into Patients table and not in PatientAddress table.

When I POST a new patient with Address then automatically it should create a new record in PatientAddress table with patnt_refno referencing to the Patient table. Could someone please assist to proceed further.

Below is my code.

POST Input:
    {
        "Address": [
            {
                "line": "3A insert Second Street",
                "city": "insert city",
                "country": "India"
            }
        ],
        "patnt_id": "P14",
        "surname": "Smith",
        "forename": "James",
        "gender": "M",
        "active": 1
    }


models.py
class Patients(models.Model):
    patnt_refno = models.AutoField(primary_key=True)
    patnt_id = models.CharField(unique=True, max_length=10, blank=True, null=True)
    surname = models.CharField(max_length=30, blank=True, null=True)
    forename = models.CharField(max_length=50, blank=True, null=True)
    gender = models.CharField(max_length=1, blank=True, null=True)
    dttm_of_birth = models.DateTimeField(blank=True, null=True)
    dttm_of_death = models.DateTimeField(blank=True, null=True)
    marital_status = models.CharField(max_length=50, blank=True, null=True)
    active = models.IntegerField()

    def __str__(self):
        return self.patnt_id

    class Meta:
        managed = False
        db_table = 'patients'

class PatientAddress(models.Model):
    addss_refno = models.AutoField(primary_key=True)
    patnt_refno = models.ForeignKey('Patients', db_column='patnt_refno', related_name='Address')
    line = models.CharField(max_length=30, blank=True, null=True)
    city = models.CharField(max_length=50, blank=True, null=True)
    state = models.CharField(max_length=50, blank=True, null=True)
    country = models.CharField(max_length=50, blank=True, null=True)
    postcode = models.CharField(max_length=50, blank=True, null=True)
    active = models.IntegerField(blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'patientaddress'


serializers.py
class PatientAddressSerializer(serializers.ModelSerializer):
    
    class Meta:
        model = PatientAddress
        fields = ('line','city','country')

class PatientsSerializer(serializers.ModelSerializer):
    Address = PatientAddressSerializer(read_only=True,many=True)
    
    class Meta:
        model = Patients
        fields = ('Address','patnt_refno','patnt_id','surname','forename','gender','active')


views.py
class PatientsList(APIView):
    
    def get(self, request):
        patients = Patients.objects.filter(gender='M',active=1).order_by('dttm_of_birth').reverse()
        serializer = PatientsSerializer(patients, many=True)
        return Response(serializer.data)
    
    def post(self, request):
        serializer = PatientsSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

urls.py
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^patients/$', views.PatientsList.as_view()),
]



Thanks in advance,
Thiagu Palaniappan

Daniel Germano Travieso

unread,
May 21, 2018, 6:11:57 PM5/21/18
to django...@googlegroups.com
Hello!

​As my initial understanding, you are attempting to use some sort of Django REST Framework.
Check the django-rest-framework documentation for some insights: http://www.django-rest-framework.org/api-guide/serializers/

You will need to overwrite the .create method of the Patient Model and the .create method of the Serializer.

Hope it helps!​


[]'s
Daniel Germano Travieso
Engenharia da Computação Turma: 013
Unicamp

--
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...@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/7a69934b-f4c7-4c85-9107-25062187a1a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages