How to Specify the structure of the payload and Specify headers in every request Django URLS
Below are my urls.py I need to Specify the structure of the payload and Specify headers in every request
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register("patient", views.PatientsApiView)
urlpatterns = [
url(r'', include(router.urls))
]
Below are my views.py
I have made 1 view with model viewset and the other with the generic API view
class PatientsApiView(viewsets.ModelViewSet):
model = Patient
fields = ("id", "first_name", "last_name", "phone", "email", "created_at")
"""
Create a serializer in serializer.py
class EmbryoSerializer(serializers.ModelSerializer):
class Meta:
model = Embryo
fields = ("id", "name", "analysis_result", "created_at", "patient")
"""
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = EmbryoSerializer(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)
def put(self, request, pk, format=None):
embryo = self.get_object(pk)
serializer = EmbryoSerializer(embryo, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, user_id):
user = User.objects.get(id=user_id)
serializer = EmbryoSerializer(embryo, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
router.register("embryo", views.EmbroApiView)
How do you think I can achieve this in my views.py
django python-3.x django-rest-framework django-urls
|
show 1 more comment
Below are my urls.py I need to Specify the structure of the payload and Specify headers in every request
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register("patient", views.PatientsApiView)
urlpatterns = [
url(r'', include(router.urls))
]
Below are my views.py
I have made 1 view with model viewset and the other with the generic API view
class PatientsApiView(viewsets.ModelViewSet):
model = Patient
fields = ("id", "first_name", "last_name", "phone", "email", "created_at")
"""
Create a serializer in serializer.py
class EmbryoSerializer(serializers.ModelSerializer):
class Meta:
model = Embryo
fields = ("id", "name", "analysis_result", "created_at", "patient")
"""
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = EmbryoSerializer(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)
def put(self, request, pk, format=None):
embryo = self.get_object(pk)
serializer = EmbryoSerializer(embryo, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, user_id):
user = User.objects.get(id=user_id)
serializer = EmbryoSerializer(embryo, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
router.register("embryo", views.EmbroApiView)
How do you think I can achieve this in my views.py
django python-3.x django-rest-framework django-urls
Use a middleware, or specify them on a case by case basis in your views.
– spectras
Nov 12 at 13:11
@spectras Is it possible to achieve this urls.py. I don't have to use routers if I can achieve this in urls.py
– SamirTendulkar
Nov 12 at 13:14
It is not, and it is not whaturls.pyis for. Its purpose is to describe your urls, it should do that and nothing more.
– spectras
Nov 12 at 13:16
@spectras I have added my views above. Is it possible to show in code how I can Specify the structure of the payload and Specify headers in every request. I have 2 types of views just in case
– SamirTendulkar
Nov 12 at 13:23
Payload you already do, since serializers will validate it. Headers you can simply pass toResponse(). For instancereturn Response( … , headers={'My-Header': 'hello'})will add the header.
– spectras
Nov 12 at 13:26
|
show 1 more comment
Below are my urls.py I need to Specify the structure of the payload and Specify headers in every request
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register("patient", views.PatientsApiView)
urlpatterns = [
url(r'', include(router.urls))
]
Below are my views.py
I have made 1 view with model viewset and the other with the generic API view
class PatientsApiView(viewsets.ModelViewSet):
model = Patient
fields = ("id", "first_name", "last_name", "phone", "email", "created_at")
"""
Create a serializer in serializer.py
class EmbryoSerializer(serializers.ModelSerializer):
class Meta:
model = Embryo
fields = ("id", "name", "analysis_result", "created_at", "patient")
"""
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = EmbryoSerializer(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)
def put(self, request, pk, format=None):
embryo = self.get_object(pk)
serializer = EmbryoSerializer(embryo, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, user_id):
user = User.objects.get(id=user_id)
serializer = EmbryoSerializer(embryo, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
router.register("embryo", views.EmbroApiView)
How do you think I can achieve this in my views.py
django python-3.x django-rest-framework django-urls
Below are my urls.py I need to Specify the structure of the payload and Specify headers in every request
from django.conf.urls import url, include
from rest_framework.routers import DefaultRouter
from . import views
router = DefaultRouter()
router.register("patient", views.PatientsApiView)
urlpatterns = [
url(r'', include(router.urls))
]
Below are my views.py
I have made 1 view with model viewset and the other with the generic API view
class PatientsApiView(viewsets.ModelViewSet):
model = Patient
fields = ("id", "first_name", "last_name", "phone", "email", "created_at")
"""
Create a serializer in serializer.py
class EmbryoSerializer(serializers.ModelSerializer):
class Meta:
model = Embryo
fields = ("id", "name", "analysis_result", "created_at", "patient")
"""
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data)
def post(self, request, format=None):
serializer = EmbryoSerializer(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)
def put(self, request, pk, format=None):
embryo = self.get_object(pk)
serializer = EmbryoSerializer(embryo, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, user_id):
user = User.objects.get(id=user_id)
serializer = EmbryoSerializer(embryo, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
router.register("embryo", views.EmbroApiView)
How do you think I can achieve this in my views.py
django python-3.x django-rest-framework django-urls
django python-3.x django-rest-framework django-urls
edited Nov 12 at 13:22
asked Nov 12 at 13:04
SamirTendulkar
18111
18111
Use a middleware, or specify them on a case by case basis in your views.
– spectras
Nov 12 at 13:11
@spectras Is it possible to achieve this urls.py. I don't have to use routers if I can achieve this in urls.py
– SamirTendulkar
Nov 12 at 13:14
It is not, and it is not whaturls.pyis for. Its purpose is to describe your urls, it should do that and nothing more.
– spectras
Nov 12 at 13:16
@spectras I have added my views above. Is it possible to show in code how I can Specify the structure of the payload and Specify headers in every request. I have 2 types of views just in case
– SamirTendulkar
Nov 12 at 13:23
Payload you already do, since serializers will validate it. Headers you can simply pass toResponse(). For instancereturn Response( … , headers={'My-Header': 'hello'})will add the header.
– spectras
Nov 12 at 13:26
|
show 1 more comment
Use a middleware, or specify them on a case by case basis in your views.
– spectras
Nov 12 at 13:11
@spectras Is it possible to achieve this urls.py. I don't have to use routers if I can achieve this in urls.py
– SamirTendulkar
Nov 12 at 13:14
It is not, and it is not whaturls.pyis for. Its purpose is to describe your urls, it should do that and nothing more.
– spectras
Nov 12 at 13:16
@spectras I have added my views above. Is it possible to show in code how I can Specify the structure of the payload and Specify headers in every request. I have 2 types of views just in case
– SamirTendulkar
Nov 12 at 13:23
Payload you already do, since serializers will validate it. Headers you can simply pass toResponse(). For instancereturn Response( … , headers={'My-Header': 'hello'})will add the header.
– spectras
Nov 12 at 13:26
Use a middleware, or specify them on a case by case basis in your views.
– spectras
Nov 12 at 13:11
Use a middleware, or specify them on a case by case basis in your views.
– spectras
Nov 12 at 13:11
@spectras Is it possible to achieve this urls.py. I don't have to use routers if I can achieve this in urls.py
– SamirTendulkar
Nov 12 at 13:14
@spectras Is it possible to achieve this urls.py. I don't have to use routers if I can achieve this in urls.py
– SamirTendulkar
Nov 12 at 13:14
It is not, and it is not what
urls.py is for. Its purpose is to describe your urls, it should do that and nothing more.– spectras
Nov 12 at 13:16
It is not, and it is not what
urls.py is for. Its purpose is to describe your urls, it should do that and nothing more.– spectras
Nov 12 at 13:16
@spectras I have added my views above. Is it possible to show in code how I can Specify the structure of the payload and Specify headers in every request. I have 2 types of views just in case
– SamirTendulkar
Nov 12 at 13:23
@spectras I have added my views above. Is it possible to show in code how I can Specify the structure of the payload and Specify headers in every request. I have 2 types of views just in case
– SamirTendulkar
Nov 12 at 13:23
Payload you already do, since serializers will validate it. Headers you can simply pass to
Response(). For instance return Response( … , headers={'My-Header': 'hello'}) will add the header.– spectras
Nov 12 at 13:26
Payload you already do, since serializers will validate it. Headers you can simply pass to
Response(). For instance return Response( … , headers={'My-Header': 'hello'}) will add the header.– spectras
Nov 12 at 13:26
|
show 1 more comment
1 Answer
1
active
oldest
votes
Specifying the structure of the payload and headers for every request is best done in views.py and serializers.py
Payload will be validated in serializers . Headers you can simply pass to Response().
For instance
return Response( … , headers={'My-Header': 'hello'}) will add the header.
example:
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data, headers={'My-Header': 'Below are all the embroys for this patient'})
Is this correct?
– SamirTendulkar
Nov 12 at 14:02
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262804%2fhow-to-specify-the-structure-of-the-payload-and-specify-headers-in-every-request%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Specifying the structure of the payload and headers for every request is best done in views.py and serializers.py
Payload will be validated in serializers . Headers you can simply pass to Response().
For instance
return Response( … , headers={'My-Header': 'hello'}) will add the header.
example:
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data, headers={'My-Header': 'Below are all the embroys for this patient'})
Is this correct?
– SamirTendulkar
Nov 12 at 14:02
add a comment |
Specifying the structure of the payload and headers for every request is best done in views.py and serializers.py
Payload will be validated in serializers . Headers you can simply pass to Response().
For instance
return Response( … , headers={'My-Header': 'hello'}) will add the header.
example:
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data, headers={'My-Header': 'Below are all the embroys for this patient'})
Is this correct?
– SamirTendulkar
Nov 12 at 14:02
add a comment |
Specifying the structure of the payload and headers for every request is best done in views.py and serializers.py
Payload will be validated in serializers . Headers you can simply pass to Response().
For instance
return Response( … , headers={'My-Header': 'hello'}) will add the header.
example:
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data, headers={'My-Header': 'Below are all the embroys for this patient'})
Specifying the structure of the payload and headers for every request is best done in views.py and serializers.py
Payload will be validated in serializers . Headers you can simply pass to Response().
For instance
return Response( … , headers={'My-Header': 'hello'}) will add the header.
example:
class EmbryoApiView(APIView):
def get(self, request, format=None):
embryo = Embryo.objects.all()
serializer = EmbryoSerializer(embryo, many=True)
return Response(serializer.data, headers={'My-Header': 'Below are all the embroys for this patient'})
answered Nov 12 at 13:58
SamirTendulkar
18111
18111
Is this correct?
– SamirTendulkar
Nov 12 at 14:02
add a comment |
Is this correct?
– SamirTendulkar
Nov 12 at 14:02
Is this correct?
– SamirTendulkar
Nov 12 at 14:02
Is this correct?
– SamirTendulkar
Nov 12 at 14:02
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53262804%2fhow-to-specify-the-structure-of-the-payload-and-specify-headers-in-every-request%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Use a middleware, or specify them on a case by case basis in your views.
– spectras
Nov 12 at 13:11
@spectras Is it possible to achieve this urls.py. I don't have to use routers if I can achieve this in urls.py
– SamirTendulkar
Nov 12 at 13:14
It is not, and it is not what
urls.pyis for. Its purpose is to describe your urls, it should do that and nothing more.– spectras
Nov 12 at 13:16
@spectras I have added my views above. Is it possible to show in code how I can Specify the structure of the payload and Specify headers in every request. I have 2 types of views just in case
– SamirTendulkar
Nov 12 at 13:23
Payload you already do, since serializers will validate it. Headers you can simply pass to
Response(). For instancereturn Response( … , headers={'My-Header': 'hello'})will add the header.– spectras
Nov 12 at 13:26