Я хочу вернуть user_id
для каждого объекта, созданного с использованием post api.
Я хочу вывод в виде
[
{"info": {
"status": "SUCCESS",
"message": "User Details Successfully Uploaded"
},
"user_id": 10001
}
]
Вот мой код
Serializer.py
class UserDetailsSerializer(serializers.ModelSerializer):
"""docstring for UserDetailsSerializer"""
class Meta:
model = UserDetails
fields = ['user_id', 'user_email', 'user_full_name', 'user_token', 'patient_id', 'user_preferences']
read_only_fields = ['user_id']
Views.py
class UserDetailsViewSet(APIView):
def get(self, request):
queryset= UserDetails.objects.all()
serializer_class=UserDetailsSerializer(queryset,many=True)
return Response(serializer_class.data)
def post(self, request):
serializer_class_post=UserDetailsSerializer(data=request.data)
if serializer_class_post.is_valid():
try:
serializer_class_post.save()
return Response([{"info": {
"status": "SUCCESS",
"message": "User Details Successfully Uploaded"
},
"user_id": serializer_class_post.data
}], status=status.HTTP_201_CREATED)
except IntegrityError as e:
return Response([{"info": {
"status": "Error",
"message": "Error Uploading User Details"
}
}], status=status.HTTP_400_BAD_REQUEST)
return Response(serializer_class_post.errors, status=status.HTTP_400_BAD_REQUEST)`
models.py
class UserDetails(models.Model):
user_id = models.IntegerField(primary_key= True)
user_email = models.CharField(max_length=254)
user_full_name = models.CharField(max_length=200)
user_token = models.CharField(max_length=200)
patient_id = models.CharField(max_length=12, unique=True)
user_preferences = models.CharField(max_length=200)
class Meta:
managed = False
db_table = 'walnut_users'
def __str__(self):
return self.user_id