How to Specify the structure of the payload and Specify headers in every request Django URLS












0














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










share|improve this question
























  • Use a middleware, or specify them on a case by case basis in your views.
    – spectras
    Nov 12 '18 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 '18 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 '18 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 '18 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 '18 at 13:26
















0














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










share|improve this question
























  • Use a middleware, or specify them on a case by case basis in your views.
    – spectras
    Nov 12 '18 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 '18 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 '18 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 '18 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 '18 at 13:26














0












0








0







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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 13:22







SamirTendulkar

















asked Nov 12 '18 at 13:04









SamirTendulkarSamirTendulkar

19711




19711












  • Use a middleware, or specify them on a case by case basis in your views.
    – spectras
    Nov 12 '18 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 '18 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 '18 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 '18 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 '18 at 13:26


















  • Use a middleware, or specify them on a case by case basis in your views.
    – spectras
    Nov 12 '18 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 '18 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 '18 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 '18 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 '18 at 13:26
















Use a middleware, or specify them on a case by case basis in your views.
– spectras
Nov 12 '18 at 13:11




Use a middleware, or specify them on a case by case basis in your views.
– spectras
Nov 12 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 13:26












1 Answer
1






active

oldest

votes


















0














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'})





share|improve this answer





















  • Is this correct?
    – SamirTendulkar
    Nov 12 '18 at 14:02











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
});


}
});














draft saved

draft discarded


















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









0














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'})





share|improve this answer





















  • Is this correct?
    – SamirTendulkar
    Nov 12 '18 at 14:02
















0














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'})





share|improve this answer





















  • Is this correct?
    – SamirTendulkar
    Nov 12 '18 at 14:02














0












0








0






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'})





share|improve this answer












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'})






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 13:58









SamirTendulkarSamirTendulkar

19711




19711












  • Is this correct?
    – SamirTendulkar
    Nov 12 '18 at 14:02


















  • Is this correct?
    – SamirTendulkar
    Nov 12 '18 at 14:02
















Is this correct?
– SamirTendulkar
Nov 12 '18 at 14:02




Is this correct?
– SamirTendulkar
Nov 12 '18 at 14:02


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation