gpt4 book ai didi

python - 使用 django rest 框架自定义 lookup_field,无法解析细节

转载 作者:太空狗 更新时间:2023-10-30 01:14:05 25 4
gpt4 key购买 nike

我得到的错误是

ImproperlyConfigured at /usercombo/
Could not resolve URL for hyperlinked relationship using view name "usercombo-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.

在我看来.py

class UserComboViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows UserCombo to be viewed or edited.
"""
queryset = UserCombo.objects.all()
serializer_class = UserComboSerializer
permission_classes = (IsAuthenticated,EmailConfirmationPermission)
lookup_field='customURL'

def get_queryset(self):
return UserCombo.objects.filter(user=self.request.user)

在我的 serializer.py 中:

class UserCombo(serializers.HyperlinkedModelSerializer):
customURL = serializers.CharField(
required=False,)

data = JSONSerializerField()

class Meta:
model = UserCombo
unique_together = (("product", "user"),)
fields = ('url', 'user', 'product', 'data', 'customURL', 'is_active')
lookup_field ='customURL'

如果我删除两条 lookup_field 行,一切正常,但 usercombo 对象的 lookup_field 是 pk。

最佳答案

在 django-rest-framework 版本 3.2 中,使用 lookup_field 作为 ModelSerializer.Meta 的选项似乎已被删除。此错误是由于此删除。仍然可以将 lookup_field 与 extra_kwargs 一起使用。

http://www.django-rest-framework.org/topics/3.2-announcement/

关于python - 使用 django rest 框架自定义 lookup_field,无法解析细节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32510032/

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