Django api call in view unable to save for foreign key userId

14 views
Skip to first unread message

chern...@gmail.com

unread,
Jan 29, 2018, 10:31:33 PM1/29/18
to Django users

What i am doing is django 2 project call api to django 1 project to book an appointment in Appointment table and save the details into BookAppt table.


I don't know what is the problem as i did say same post with the same value to that BookAppt table API and it works normally. But when saving using the API call, it gave me this error.

{"patientId":["This field is required."]}.


I have no idea why as i did put in the patientId field. Please help as i have been stuck at this part for 2 days now.


Here is my code:


Do note that it is django 2 calling to django 1


model.py

django 1

class Appointments (models.Model):
    patientId = models.IntegerField()
    clinicId = models.CharField(max_length=10)
    date = models.DateField()
    time = models.TimeField()
    created = models.DateTimeField(auto_now_add=True)
    ticketNo = models.IntegerField()

    STATUS_CHOICES = (
        ("Booked", "Booked"),
        ("Done", "Done"),
        ("Cancelled", "Cancelled"),
    )
    status = models.CharField(max_length=20, choices=STATUS_CHOICES, default="Booked")

django 2

class MyUser(AbstractUser):
    userId = models.AutoField(primary_key=True)
    gender = models.CharField(max_length=6, blank=True, null=True)
    nric = models.CharField(max_length=9, blank=True, null=True)
    birthday = models.DateField(blank=True, null=True)
    birthTime = models.TimeField(blank=True, null=True)

class BookAppt(models.Model):
    clinicId = models.CharField(max_length=20)
    patientId = models.ForeignKey(MyUser, on_delete=models.CASCADE)
    scheduleTime = models.DateTimeField()
    ticketNo = models.CharField(max_length=5)
    status = models.CharField(max_length=20)

serializer

django 1

class AppointmentsSerializer(serializers.ModelSerializer):

    class Meta:
        model = Appointments
        fields = ('id', 'patientId', 'clinicId', 'date', 'time', 'created', 'ticketNo', 'status')

django 2

class MyUserSerializer(serializers.ModelSerializer):

    class Meta:
        model = MyUser
        fields = ('userId', 'username', 'email', 'first_name', 'last_name', 'gender', 'nric', 'birthday', 'birthTime')
        read_only_fields = ('userId',)


class BookApptSerializer(serializers.ModelSerializer):
    patientId = MyUserSerializer(many=False)

    class Meta:
        model = BookAppt
        fields = ('id', 'patientId', 'clinicId', 'scheduleTime', 'ticketNo', 'status')

view.py

django 1

class AppointmentsViewSet(viewsets.ModelViewSet):
    permission_classes = [AllowAny]
    queryset = Appointments.objects.all()
    serializer_class = AppointmentsSerializer

django 2

@csrf_exempt
def my_django_view(request):

    if request.method == 'POST':
        r = requests.post('http://127.0.0.1:8000/api/makeapp/', data=request.POST)
    else:
        r = requests.get('http://127.0.0.1:8000/api/makeapp/', data=request.GET)

    if r.status_code == 201 and request.method == 'POST':
        data = r.json()
        patient = request.data['patientId']
        patientId = MyUser.objects.get(id=patient)

        saveget_attrs = {
            "patientId": patientId,
            "clinicId": data["clinicId"],
            "scheduleTime": data["created"],
            "ticketNo": data["ticketNo"],
            "status": data["status"],
        }
        saving = BookAppt.objects.create(**saveget_attrs)

        return HttpResponse(r.text)
    elif r.status_code == 200:  # GET response
        return HttpResponse(r.json())
    else:
        return HttpResponse(r.text)


class BookApptViewSet(viewsets.ModelViewSet):
    permission_classes = [AllowAny]
    queryset = BookAppt.objects.all()
    serializer_class = BookApptSerializer

Etienne Robillard

unread,
Jan 30, 2018, 12:29:16 PM1/30/18
to chern...@gmail.com, django...@googlegroups.com

Hi,


Le 2018-01-29 à 22:31, chern...@gmail.com a écrit :
{"patientId":["This field is required."]}.

This is a generic form validation error. I suspect the problem is with the following code:

class BookApptSerializer(serializers.ModelSerializer):
    patientId = MyUserSerializer(many=False)
Perhaps you could trace the error in serializers.ModelSerializer by using pdb ?

Etienne

-- 
Etienne Robillard
tka...@yandex.com
https://www.isotopesoftware.ca/
Reply all
Reply to author
Forward
0 new messages