Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix the format_errors bug when response data is None#1229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ericinfins wants to merge3 commits intodjango-json-api:main
base:main
Choose a base branch
Loading
fromericinfins:fix/empty-data-in-response-using-rest-action
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletionsexample/tests/unit/test_default_drf_serializers.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -104,6 +104,19 @@ def test_blog_create(client):
assert resp.json() == expected


@pytest.mark.django_db
def test_rest_action_request(client):
url = reverse("drf-entry-blog-list") + "/actions/custom_response"
name = "Dummy Name"
request_data = {
"data": {"attributes": {"name": name}, "type": "blogs"},
}

resp = client.post(url, request_data)

assert resp.status_code == 400


@pytest.mark.django_db
def test_get_object_gives_correct_blog(client, blog, entry):
url = reverse("drf-entry-blog-detail", kwargs={"entry_pk": entry.id})
Expand Down
18 changes: 15 additions & 3 deletionsexample/views.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
import logging
import rest_framework.exceptions as exceptions
import rest_framework.parsers
import rest_framework.renderers
from django_filters import rest_framework as filters
from rest_framework.filters import SearchFilter

from rest_framework.response import Response
from rest_framework.decorators import action
import rest_framework_json_api.metadata
import rest_framework_json_api.parsers
import rest_framework_json_api.renderers
Expand DownExpand Up@@ -74,6 +76,18 @@ def get_object(self):

return super().get_object()

@action(url_path="actions/custom_response", detail=False, methods=["post"])
def custom_response(self, request):
data = request.data
serializer = self.get_serializer(data=data)
serializer.is_valid(raise_exception=True)
self.perform_create(serializer)
try:
test = 1/0
except Exception:
return Response(status=400)

return Response(status=201)

class JsonApiViewSet(ModelViewSet):
"""
Expand DownExpand Up@@ -257,7 +271,6 @@ def get_queryset(self, *args, **kwargs):

return queryset


class CompanyViewset(ModelViewSet):
queryset = Company.objects.all()
serializer_class = CompanySerializer
Expand All@@ -276,7 +289,6 @@ class ProjectTypeViewset(ModelViewSet):
class EntryRelationshipView(RelationshipView):
queryset = Entry.objects.all()


class BlogRelationshipView(RelationshipView):
queryset = Blog.objects.all()

Expand Down
2 changes: 1 addition & 1 deletionrest_framework_json_api/utils.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -474,6 +474,6 @@ def format_error_object(message, pointer, response):


def format_errors(data):
iflen(data) > 1andisinstance(data, list):
ifisinstance(data, list)andlen(data) > 1:
data.sort(key=lambda x: x.get("source", {}).get("pointer", ""))
return {"errors": data}

[8]ページ先頭

©2009-2025 Movatter.jp