gpt4 book ai didi

Django - DRF 删除/检索/补丁返回 404 { 详细信息 : "Not found" }

转载 作者:行者123 更新时间:2023-12-03 19:35:51 25 4
gpt4 key购买 nike

经过大约4-5小时的密集调试。
我放弃了寻找导致这个,可能非常简单的错误的原因。
使用 Update 和 Patch/Put Mixins 进行了尝试,但效果不佳。
由于查询集是100%,但我找不到问题?
尝试使用 .get() 和我能想到的所有其他东西。

我的观点非常简单:

class RemoveModel3D(generics.DestroyAPIView):

serializer_class = Model3DSerializer

def get_queryset(self):
user_pk = self.kwargs["pk"]
return Model3D.objects.filter(owners__in=[user_pk])

附言该查询集与 ListModelMixin 一起工作得非常好。我读到 List 用于集合,而 Retrieve/Destroy/Update 用于单个模型实例,但是如何使查询集成为单个模型实例?我在任何地方都找不到

编辑:添加与问题相关的序列化程序和模型片段

序列化器:
class Model3DSerializer(serializers.ModelSerializer):

User = get_user_model()

commits = CommitSerializer(many=True, required=False, read_only=True)
favorited_by = UserSerializer(many=True, required=False, read_only=True)

date_uploaded = serializers.DateTimeField(read_only=True)
owners = serializers.PrimaryKeyRelatedField(many=True, read_only=True)

class Meta:
model = Model3D
fields = (
'id',
'title',
'owners',
'description',
'date_uploaded',
'favorited_by',
'commits'
)

模型:
class Model3D(models.Model):

title = models.CharField(max_length=64)
# Many models many owners, seems reasonable to me
owners = models.ManyToManyField(User, related_name='owners')
description = models.TextField(null=True)
date_uploaded = models.DateTimeField(auto_now_add=True)

# Many models many people who like them.
favorited_by = models.ManyToManyField(User, related_name='favorited_by')

意见:
class ListAllModels3D(generics.ListAPIView):

serializer_class = Model3DSerializer

def get_queryset(self):
queryset = Model3D.objects.all()
model_id = self.request.query_params.get('id', None)

if model_id is not None:
queryset = queryset.filter(pk=model_id)

return queryset

class RemoveModel3D(generics.DestroyAPIView):

serializer_class = Model3DSerializer

def get_queryset(self):
queryset = Model3D.objects.all()
model_id = self.request.query_params.get('id', None)

if model_id is not None:
queryset = queryset.filter(pk=model_id)

return queryset


class Models3D( mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView,
):

serializer_class = Model3DSerializer

def get_queryset(self):
user_pk = self.kwargs["pk"]
return Model3D.objects.filter(owners__in=[user_pk])

def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)

def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)

def perform_create(self, serializer):
# FIXME: this is a bad way to set the value, but ...
user_id = self.kwargs["pk"]
serializer.validated_data['owners'] = [user_id]

serializer.save()

最佳答案

默认lookup_url_kwarg对于 GenericApiView 是 pk .但是您正在使用它来过滤 User ,因此您在查询 Owner 和 Model3D 时将使用相同的 pk 值。

结果类似于这个伪代码,除非用户和model3d有相同的pk,否则会导致404响应。

kwargs = {'pk': 20} 
try:
Model3D.objects.get(owners__in=[kwargs['pk']], pk=kwargs['pk'])
except ObjectNotFound:
raise Http404Exception('Not found')

要解决此问题,请使用支持嵌套 api 路由的插件。或者您可以覆盖 View 集 get_object方法,这是引发 404 的地方。
def get_object(self):
queryset = self.get_queryset()
pk = self.request.query_params.get('id', None)
obj = get_object_or_404(queryset, pk=pk)
self.check_object_permissions(self.request, obj)
return obj

http://www.django-rest-framework.org/api-guide/generic-views/#get_objectself
get_object方法由 retrieve() 使用(获取) update() PATCH/PUT 和 destroy() (DELETE) 并且默认是使用 self.kwargs['pk']作为查找值。

关于Django - DRF 删除/检索/补丁返回 404 { 详细信息 : "Not found" },我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49331003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com