This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_users(request): | |
| FIELDS = ['first_name', 'last_name', 'last_login', 'phone', 'photo', 'email', 'id'] | |
| users = list(User.objects.values(*FIELDS)) | |
| for user in users: | |
| groups = list(user.groups.values('id', 'name')) | |
| user['groups'] = groups | |
| user['is_birthday_soon'] = user.is_birthday_in_this_month() | |
| return JsonResponse(users, safe=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_users(request): | |
| users = list(User.objects.values('first_name', 'last_name', 'email', 'id')) | |
| return JsonResponse(users, safe=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def get_users(request): | |
| users = list(User.objects.values(‘first_name’, ‘id’)) | |
| return JsonResponse(users, safe=False) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionsAPIView(generics.ListCreateAPIView): | |
| filter_backends = (DynamicSearchFilter,) | |
| queryset = Question.objects.all() | |
| serializer_class = QuestionSerializer |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class DynamicSearchFilter(filters.SearchFilter): | |
| def get_search_fields(self, view, request): | |
| return request.GET.getlist('search_fields', []) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionSerializer(serializers.ModelSerializer): | |
| choice_set = ChoiceSerializer(many=True) | |
| class Meta: | |
| model = Question | |
| fields = '__all__' | |
| def create(self, validated_data): | |
| choice_validated_data = validated_data.pop('choice_set') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionSerializer(serializers.ModelSerializer): | |
| choice_set = ChoiceSerializer(many=True) | |
| class Meta: | |
| model = Question | |
| fields = ('pub_date', 'question_text', 'choice_set') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ChoiceSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = Choice | |
| fields = ('choice_text',) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionsView(APIView): | |
| def post(self, request, *args, **kwargs): | |
| serializer = QuestionSerializer(data=request.data) | |
| if serializer.is_valid(): | |
| question = serializer.save() | |
| serializer = QuestionSerializer(question) | |
| return Response(serializer.data, status=status.HTTP_201_CREATED) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class QuestionSerializer(serializers.ModelSerializer): | |
| class Meta: | |
| model = Question | |
| fields = ('pub_date', 'question_text', 'choice_set') |