Benjamin SOULAS
2018-08-14 09:16:02 UTC
Hi everyone, I am relatively new to DRF,
I try to implement an API in which I use pymongo to insert my data in DB,
but without the ORM. I have implemented a View as follows:
import traceback
from api.EmsSerializer.ProductSerializer import ProductSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
class ProductRecordView(APIView):
"""
A class based view for creating and fetching product records
"""
def get(self, request):
"""
"""
print(*args)
print(**kwargs)
print(request)
def post(self, request):
"""
@brief
"""
try:
# JSON data retrieval
wData = request.data
# Serializer instanciation
wSerializer = ProductSerializer()
# Validation checkings: @TODO
# Product creation
wCreatedProductDict = wSerializer.Create(aData=wData)
# response return
print("VIEWS: wCreatedproduct")
print(wCreatedProductDict)
return Response(wCreatedProductDict, status=status.HTTP_201_CREATED)
except Exception:
traceback.print_exc()
return Response(status=status.HTTP_400_BAD_REQUEST)
And the object I use is Product:
# coding: utf-8
class Product(object):
def __init__(self, aActiveComm, aName, aImg, aSettings, aSourceId, aCommStatus, aActivated, aZoneId, aChannels,
aServices, aSettingStatus, aCommSettings, aHistory, aLandmark, aUsageId, aCreationDate, aType,
aCabinetId, aDescription):
self._id = None
self.activeComm = aActiveComm
self.name = aName
self.img = aImg
self.settings = aSettings
self.sourceId = aSourceId
self.commStatus = aCommStatus
self.activated = aActivated
self.zoneId = aZoneId
self.channels = aChannels
self.services = aServices
self.settingStatus = aSettingStatus
self.commSettings = aCommSettings
self.history = aHistory
self.landmark = aLandmark
self.usageId = aUsageId
self.creationDate = aCreationDate
self.type = aType
self.cabinetId = aCabinetId
self.description = aDescription
def ToDict(self):
"""
ToDict
@brief: Return object in dict format
:return: Dict
"""
return self.__dict__
Here is my url.py
from django.conf.urls import url
from django.contrib import admin
from django.urls import path
from api import views
urlpatterns = [
url('admin/', admin.site.urls),
url('api/products/', views.ProductRecordView.as_view()),
url(r'^api/products/(?P<product_id>\d+)$', views.ProductRecordView.as_view()),
]
In POST request, it work like a charm, But if I want to try a GET request,
I don't even know how to get the ID defined in the url
Maybe it is trivial, but I really need help ...
Kind regards
I try to implement an API in which I use pymongo to insert my data in DB,
but without the ORM. I have implemented a View as follows:
import traceback
from api.EmsSerializer.ProductSerializer import ProductSerializer
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
class ProductRecordView(APIView):
"""
A class based view for creating and fetching product records
"""
def get(self, request):
"""
"""
print(*args)
print(**kwargs)
print(request)
def post(self, request):
"""
@brief
"""
try:
# JSON data retrieval
wData = request.data
# Serializer instanciation
wSerializer = ProductSerializer()
# Validation checkings: @TODO
# Product creation
wCreatedProductDict = wSerializer.Create(aData=wData)
# response return
print("VIEWS: wCreatedproduct")
print(wCreatedProductDict)
return Response(wCreatedProductDict, status=status.HTTP_201_CREATED)
except Exception:
traceback.print_exc()
return Response(status=status.HTTP_400_BAD_REQUEST)
And the object I use is Product:
# coding: utf-8
class Product(object):
def __init__(self, aActiveComm, aName, aImg, aSettings, aSourceId, aCommStatus, aActivated, aZoneId, aChannels,
aServices, aSettingStatus, aCommSettings, aHistory, aLandmark, aUsageId, aCreationDate, aType,
aCabinetId, aDescription):
self._id = None
self.activeComm = aActiveComm
self.name = aName
self.img = aImg
self.settings = aSettings
self.sourceId = aSourceId
self.commStatus = aCommStatus
self.activated = aActivated
self.zoneId = aZoneId
self.channels = aChannels
self.services = aServices
self.settingStatus = aSettingStatus
self.commSettings = aCommSettings
self.history = aHistory
self.landmark = aLandmark
self.usageId = aUsageId
self.creationDate = aCreationDate
self.type = aType
self.cabinetId = aCabinetId
self.description = aDescription
def ToDict(self):
"""
ToDict
@brief: Return object in dict format
:return: Dict
"""
return self.__dict__
Here is my url.py
from django.conf.urls import url
from django.contrib import admin
from django.urls import path
from api import views
urlpatterns = [
url('admin/', admin.site.urls),
url('api/products/', views.ProductRecordView.as_view()),
url(r'^api/products/(?P<product_id>\d+)$', views.ProductRecordView.as_view()),
]
In POST request, it work like a charm, But if I want to try a GET request,
I don't even know how to get the ID defined in the url
Maybe it is trivial, but I really need help ...
Kind regards
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.