class getAlarmlogsFilterByDistincts(APIView):
permission_classes = (IsAuthenticated,)
def get(self, request, *args, **kwargs):
column_name = request.data['column_name'] #just an idea, dont works
distincts_list = request.data['distincts_list'] #just an idea, dont works
column_values = [Alarmlog.objects.filter(**{column_name: x}) for x in distincts_list ]
serialized_response = AlarmlogSerializer(column_values)
return Response(serialized_response.data, status=status.HTTP_200_OK)
My view is more or less like this. I need to receive a string in column_name and a list in distincts_list (maybe in json format).
What i tried:
class AlarmlogsFilterByDistinctsSerializer(serializers.Serializer):
distincts_list = serializers.ListField()
column_name = serializers.CharField()
class getAlarmlogsFilterByDistincts(CreateAPIView):
permission_classes = (IsAuthenticated,)
serializer_class = AlarmlogsFilterByDistinctsSerializer
def post(self, request, *args, **kwargs):
column_values = columnFilter(request.data['column_name'], request.data['distincts_list'])
response_serialized = serializers.serialize('json', column_values)
print(response_serialized)
return Response(response_serialized, status=status.HTTP_200_OK)
i've tried to use post method, but my response come out as a string:
"[{\"model\": \"alarms.alarmlog\", \"pk\": 26, \"fields\": {\"created_at\": \"2023-01-12T17:06:19.016Z\", \"updated_at\": \"2023-01-12T17:06:19.016Z\", \"name\": \"string43\", \"description\": \"string\", \"severity\": \"low\", \"type\": \"string\", \"period\": \"string\", \"value\": \"string\", \"reference\": \"string\", \"source\": 2147483647, \"third_party_id\": \"string\", \"device\": \"string\", \"extra_content\": {}, \"occurred_at\": \"2023-01-12T16:29:39.055Z\", \"equipment\": 7}}, {\"model\": \"alarms.alarmlog\", \"pk\": 27,...
ps. sorry my english
you could return the data in the following
import json
return Response(json.dumps(response_serialized), status=status.HTTP_200_OK)
Related
Send url: https://...entities/?ids=[1, 2, 3]
class EntityListSerializer(serializers.Serializer):
ids = serializers.ListSerializer(child=serializers.IntegerField())
class EntityListView(BaseView):
def get(self, request, *args, **kwargs):
query_params = request.query_params # <QueryDict: {'ids': ['[1, 2, 3]']}>
request_serializer = EntityListSerializer(data=request.query_params)
request_serializer.is_valid(raise_exception=True)
...
Resonse json:
{
"error": {
"ids": [
"Обязательное поле."
]
}
}
How to serialize it in right way?
Try:
class EntityListSerializer(serializers.Serializer):
ids = serializers.ListSerializer(child=serializers.IntegerField())
class EntityListView(BaseView):
def get(self, request, *args, **kwargs):
query_params = request.query_params
request_serializer = EntityListSerializer(data=query_params.dict())
request_serializer.is_valid(raise_exception=True)
...
As I do POST method, it returns the whole array as a response. Is it possible to return for example only the ID after successful request?
I have
{ "requestid": 1
"requestname": "Sample request",
"projectmanager": "Josh",
"creationdate": "2022-09-26T23:48:00Z" }
What if I only want to return the result as
{ "requestid": 1 }
Here's my view for reference:
class CreateRequestView(generics.CreateAPIView):
queryset = requestTable.objects.all()
serializer_class = RequestSerializer
Method create is perfect for the job. Firstfully get what a generic response returns, then modify the view's response as you please.
class CreateRequestView(generics.CreateAPIView):
queryset = requestTable.objects.all()
serializer_class = RequestSerializer
def create(self, request, *args, **kwargs):
response = super().create(request, *args, **kwargs)
return Response({"requestid": response.data['requestid']})
You can do this with JsonResponse
from django.http import JsonResponse
def your_view():
...
your_id = 1
return JsonResponse({'requestid':your_id})
I have defined the following code in the views.py I am getting
TypeError : Object of type function is not JSON serializable
Libaries:
from django.db.models.query import QuerySet
from django.shortcuts import render
from django.http import HttpResponse , Http404 , JsonResponse
import json
from json import JSONEncoder
from FeedPost.models import trill
def home(request, *args, **kwargs):
return render(request, "feed.html", context={}, status=200)
Where the error occured
def trills_list_view(request, *args , **kwargs):
qs = trill.objects.all()
trills_list = [{"id": x.id, "name": x.name} for x in qs]
data = {
"response": trill_list
}
return JsonResponse(data)
def trill_list(request, trill_id, *args, **kwargs):
data = {
"id" : trill_id,
}
status = 200
try:
obj = trill.objects.get(id=trill_id)
data['name'] =obj.name
except:
data['message'] = "Not found"
status = 404
return JsonResponse(data, status=status)
Unable to store all the data into Django database. Its printing the correct list of data from an open API but not storing.
But if i give direct values of data1 in ( client.py ) for eg:
data1 = {
'employee_name':'Name',
'employee_salary':'20000',
'employee_age':'22',
'profile_image':' '
}
Then it store the above dictionary values into Django database.But the data i am getting using requests is a list.It prints in cmd but doesn't store in DB.
client.py file
def get_alldata():
url1 = "http://dummy.restapiexample.com/api/v1/employees"
url = "http://127.0.0.1:8000/employee/"
my_response = requests.get(url1)
token = get_token()
header = {'Authorization' : f'Token {get_token()}'}
data1 = [ my_response.json() ]
for d in range(len(data1)):
payload = data1[d]
res = requests.post(url, data=data1[d] , headers=header )
print(data1[d])
get_alldata()
This is the Api.py file in which includes the get and post method using serializers in django rest framework.
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.authtoken.models import Token
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from employee.serializers import *
class EmployeeAuthentication(ObtainAuthToken):
def post(self,request, *args, **kwargs):
serializer = self.serializer_class(data=request.data, context={'request':request})
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']
token , created = Token.objects.get_or_create(user=user)
return Response(token.key)
class EmployeeView(APIView):
def get(self, request):
model = Employee.objects.all()
serializer = EmployeeSerializer(model, many=True)
return Response(serializer.data)
def post(self, request):
serializer = EmployeeSerializer(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)
I got the solution.There is no problem in code yet.Problem was the data i was getting from an open API contains a Null value in a field which was unable to store into Django database.If anyone having same issue, don't GET any field having null value.
Docs says only mapping of GET
user_list = UserViewSet.as_view({'get': 'list'})
user_detail = UserViewSet.as_view({'get': 'retrieve'})
tests.py:
def test_admin_can_create_role(userprofiles, aoo_admin, bug_manager, note_admin):
aoo = User.objects.get(username='aoo')
factory = APIRequestFactory()
view = RoleViewSet.as_view()
url = reverse('api:role-list')
data = {
'name': 'FirstAdmin',
'type': Role.RoleType.admin,
'company': 1,
}
request = factory.post(url, data=data, format='json')
force_authenticate(request, user=aoo)
response = view(request)
assert 201 == response.data
viewsets.py
class RoleViewSetPermission(permissions.BasePermission):
message = 'Only Manager or Admin are allowed'
def has_permission(self, request, view):
user = request.user
return user.has_perm('roles.add_role') \
and user.has_perm('roles.change_role') \
and user.has_perm('roles.delete_role')
class RoleViewSet(viewsets.ModelViewSet):
permission_classes = (RoleViewSetPermission,)
queryset = Role.objects.all()
serializer_class = RoleSerializer
filter_backends = (filters.DjangoFilterBackend, SearchFilter)
filter_class = RoleFilter
search_fields = ('name', 'description', 'user__username', 'company__name', 'company__name_th')
def filter_queryset(self, queryset):
try:
company = self.request.user.user_profile.companyappid.company
except AttributeError:
logger.error(f'{self.request.user} has AttributeError')
return Role.objects.none()
else:
logger.info(f'{self.request.user} is {company} member')
return queryset.filter(company=company)
Trackback:
cls = <class 'poinkbackend.apps.roles.api.viewsets.RoleViewSet'>, actions = None, initkwargs = {}
#classonlymethod
def as_view(cls, actions=None, **initkwargs):
"""
Because of the way class based views create a closure around the
instantiated view, we need to totally reimplement `.as_view`,
and slightly modify the view function that is created and returned.
"""
# The suffix initkwarg is reserved for identifying the viewset type
# eg. 'List' or 'Instance'.
cls.suffix = None
# actions must not be empty
if not actions:
> raise TypeError("The `actions` argument must be provided when "
"calling `.as_view()` on a ViewSet. For example "
"`.as_view({'get': 'list'})`")
E TypeError: The `actions` argument must be provided when calling `.as_view()` on a ViewSet. For example `.as_view({'get': 'list'})`
../../.pyenv/versions/3.6.3/envs/poink/lib/python3.6/site-packages/rest_framework/viewsets.py:55: TypeError
Question:
How to do force_authenticate and request.post to the viewsets?
I have no problem with get. It has an answer already in the SO
References:
http://www.django-rest-framework.org/api-guide/viewsets/
I have to use APIClient not APIRequestFactory.
I though it has only one way to do testing.
Here is my example.
def test_admin_can_create_role(userprofiles, aoo_admin, bug_manager, note_admin):
aoo = User.objects.get(username='aoo')
client = APIClient()
client.force_authenticate(user=aoo)
url = reverse('api:role-list')
singh = Company.objects.get(name='Singh')
data = {
'name': 'HairCut',
'type': Role.RoleType.admin,
'company': singh.id, # Must be his companyid. Reason is in the RoleSerializer docstring
}
response = client.post(url, data, format='json')
assert 201 == response.status_code