马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 mcdmcd 于 2021-3-20 04:30 编辑
项目环境: python3.9 Django2.2
今天在学习Django rest framework框架
使用HyperlinkedModelSerializer 时 fields 加上‘url‘后,在用POST方法添加数据后就回报错:
Could not resolve URL for hyperlinked relationship using view name "game-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
serializer.pyclass GameSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Game
fields = ('url', 'g_name', 'g_price')
views.pyfrom rest_framework.generics import ListCreateAPIView, RetrieveUpdateDestroyAPIView
from App.models import Game
from App.serializers import GameSerializer
# Create your views here.
class GamesView(ListCreateAPIView):
serializer_class = GameSerializer
# model = Game
queryset = Game.objects.all()
class GameView(RetrieveUpdateDestroyAPIView):
serializer_class = GameSerializer
queryset = Game.objects.all()
urls.pyfrom django.urls import path
from App import views
app_name = 'App'
urlpatterns = [
path('games/', views.GamesView.as_view(), ),
path('games/<int:pk>/', views.GameView.as_view(), name='game-detail'),
]
|