我正在将DRF从泛型迁移到视图集,但收到此错误:
Could not resolve URL for hyperlinked relationship using view name "monument-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
这是我的urls.py
from app.api import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register(r'monuments', views.MonumentsViewSet)
urlpatterns = router.urls
这是我的观点
from app.api import serializers
from rest_framework import viewsets
class MonumentsViewSet(viewsets.ModelViewSet):
serializer_class = serializers.MonumentSerializer
queryset = serializer_class.Meta.model.objects.all()
lookup_field = 'id'
最后是我的序列化器
from app.api import models
from rest_framework import serializers
class MonumentSerializer(serializers.HyperlinkedModelSerializer):
images = serializers.StringRelatedField(many=True)
class Meta:
model = models.Monument
fields = '__all__'