0% found this document useful (0 votes)
24 views3 pages

Django User Health Info API

The document outlines a Django application named 'projectapi' that manages user health information. It includes models for storing user health data, serializers for data validation, and API views for retrieving and posting health information. The application is structured with URL routing to handle API requests for user health information management.

Uploaded by

Sathwik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

Django User Health Info API

The document outlines a Django application named 'projectapi' that manages user health information. It includes models for storing user health data, serializers for data validation, and API views for retrieving and posting health information. The application is structured with URL routing to handle API requests for user health information management.

Uploaded by

Sathwik
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

projectsapi/

[Link]

from [Link] import AppConfig

class ProjectapiConfig(AppConfig):
default_auto_field = '[Link]'
name = 'projectapi'

[Link]
from [Link] import models

class UserHealthInfo([Link]):
user_health_info_id = [Link](primary_key=True)
login_type_id = [Link]()
user_id = [Link]()
blood_group = [Link](max_length=5, blank=True, null=True)
allergies = [Link](blank=True, null=True)
current_medication = [Link](blank=True, null=True)
medical_conditions = [Link](blank=True, null=True)
emergency_contact_name = [Link](max_length=100, blank=True,
null=True)
emergency_contact_phone = [Link](max_length=15, blank=True,
null=True)
health_notes = [Link](blank=True, null=True)
created_at = [Link](blank=True, null=True)
updated_at = [Link](blank=True, null=True)

class Meta:
db_table = 'user_health_info'
managed = False

[Link]
from rest_framework import serializers
from .models import UserHealthInfo

class UserHealthInfoSerializer([Link]):
class Meta:
model = UserHealthInfo
fields = '__all__'

[Link]

from [Link] import path


from .views import GetUserHealthInfoView, PostUserHealthInfoView

urlpatterns = [
path('api/get-user-healthinfo/', GetUserHealthInfoView.as_view(),
name='get-user-healthinfo'),
path('api/post-user-healthinfo/', PostUserHealthInfoView.as_view(),
name='post-user-healthinfo'),
]

[Link]

from rest_framework.views import APIView


from rest_framework.response import Response
from rest_framework import status
from .models import UserHealthInfo
from .serializers import UserHealthInfoSerializer

class GetUserHealthInfoView(APIView):
def get(self, request):
login_type_id = [Link]('login_type_id')
user_id = [Link]('user_id')

if not login_type_id or not user_id:


return Response({'error': 'login_type_id and user_id
required'}, status=400)

try:
record =
[Link](login_type_id=login_type_id, user_id=user_id)
serializer = UserHealthInfoSerializer(record)
return Response([Link], status=200)
except [Link]:
return Response({'message': 'Health info not found'},
status=404)

class PostUserHealthInfoView(APIView):
def post(self, request):
serializer = UserHealthInfoSerializer(data=[Link])
if serializer.is_valid():
instance, created = [Link].update_or_create(
login_type_id=serializer.validated_data['login_type_id'],
user_id=serializer.validated_data['user_id'],
defaults=serializer.validated_data
)
return Response(UserHealthInfoSerializer(instance).data,
status=200)
return Response([Link], status=400)

MainFolder
[Link]

from [Link] import admin


from [Link] import path, include

urlpatterns = [
path('admin/', [Link]),
path('mainhub/', include('[Link]')),
]

You might also like