django-rest-framework

Tools to coverage tests Django Rest Framework

百般思念 提交于 2020-05-15 06:07:17
问题 I am studying TDD and developing an API in Django Rest Framework, and I had a need that I researched and could not find some tools to solve my problem, I am trying to find out how much my tests cover my application in %. For know the number of possibilities and possible suggestions of what is missing cover, I found the coverage lib, but it generates a report with lots of data, which are not very useful for my case, I just want to know the coverage of my tests that I created. Does anyone know

Tools to coverage tests Django Rest Framework

强颜欢笑 提交于 2020-05-15 06:04:46
问题 I am studying TDD and developing an API in Django Rest Framework, and I had a need that I researched and could not find some tools to solve my problem, I am trying to find out how much my tests cover my application in %. For know the number of possibilities and possible suggestions of what is missing cover, I found the coverage lib, but it generates a report with lots of data, which are not very useful for my case, I just want to know the coverage of my tests that I created. Does anyone know

Django rest framework filter with or condition

谁说胖子不能爱 提交于 2020-05-15 04:43:27
问题 I am using Django Rest Framework Filter to access my data. I need to get data that answers one of two conditions. Example: Mywebsite/api/animal/?name=lion||name=frog The || is not working. Does anyone know how I can do this filter? 回答1: I realize this maybe a little old but i solved it like this: from django.db.models import Q class FooViewSet(viewsets.ModelViewSet): queryset = Foo.objects.all() status = self.request.query_params.get('status', None) def get_queryset(self): if status is not

Django rest framework filter with or condition

坚强是说给别人听的谎言 提交于 2020-05-15 04:42:11
问题 I am using Django Rest Framework Filter to access my data. I need to get data that answers one of two conditions. Example: Mywebsite/api/animal/?name=lion||name=frog The || is not working. Does anyone know how I can do this filter? 回答1: I realize this maybe a little old but i solved it like this: from django.db.models import Q class FooViewSet(viewsets.ModelViewSet): queryset = Foo.objects.all() status = self.request.query_params.get('status', None) def get_queryset(self): if status is not

Django REST Framework - Set request in serializer test?

依然范特西╮ 提交于 2020-05-14 18:17:11
问题 I built a web app where the back-end is implemented using the Django REST Framework. Now I'm writing unit tests and I have come across a problem in testing my serializer methods. Here is one example of a serializer method I'm struggling with: def get_can_edit(self, obj): request = self.context.get('request') user = User.objects.get(username=request.user) return user == obj.admin When trying to call this from the test, first I declare an instance of the serializer: self.serializer =

Django REST Framework - Set request in serializer test?

[亡魂溺海] 提交于 2020-05-14 18:16:47
问题 I built a web app where the back-end is implemented using the Django REST Framework. Now I'm writing unit tests and I have come across a problem in testing my serializer methods. Here is one example of a serializer method I'm struggling with: def get_can_edit(self, obj): request = self.context.get('request') user = User.objects.get(username=request.user) return user == obj.admin When trying to call this from the test, first I declare an instance of the serializer: self.serializer =

Django Rest Framework How to update SerializerMethodField

对着背影说爱祢 提交于 2020-05-12 11:49:11
问题 I have a serializer like this: class PersonSerializer(serializers.ModelSerializer): gender = serializers.SerializerMethodField() bio = BioSerializer() class Meta: model = Person fields = UserSerializer.Meta.fields + ('gender', 'bio',) def get_gender(self, obj): return obj.get_gender_display() I used this to display "Male" and "Female"(insted of "M" of "F") while performing GET request. This works fine. But now I am writing an patch method for the model and SerializerMethodField() has read

Django Rest Framework How to update SerializerMethodField

本秂侑毒 提交于 2020-05-12 11:49:05
问题 I have a serializer like this: class PersonSerializer(serializers.ModelSerializer): gender = serializers.SerializerMethodField() bio = BioSerializer() class Meta: model = Person fields = UserSerializer.Meta.fields + ('gender', 'bio',) def get_gender(self, obj): return obj.get_gender_display() I used this to display "Male" and "Female"(insted of "M" of "F") while performing GET request. This works fine. But now I am writing an patch method for the model and SerializerMethodField() has read

How to return custom data with Access and Refresh Tokens to identify users in Django Rest Framework simple JWT?

我是研究僧i 提交于 2020-05-12 04:37:28
问题 In Django, superuser can add more user according to their roll. I'm using simple JWT with DRF for authentication. But it is impossible to detect the type of user only by seeing the Access and Refresh Tokens. Here are my settings.py file REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',), 'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',), } urls.py from django.contrib import admin from django.urls

How to return custom data with Access and Refresh Tokens to identify users in Django Rest Framework simple JWT?

橙三吉。 提交于 2020-05-12 04:36:06
问题 In Django, superuser can add more user according to their roll. I'm using simple JWT with DRF for authentication. But it is impossible to detect the type of user only by seeing the Access and Refresh Tokens. Here are my settings.py file REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated',), 'DEFAULT_AUTHENTICATION_CLASSES': ('rest_framework_simplejwt.authentication.JWTAuthentication',), } urls.py from django.contrib import admin from django.urls