1 回答

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超10個(gè)贊
我也曾經(jīng)遇到過這個(gè)問題,但我通過這樣做解決了:
urls.py(項(xiàng)目),
您可能需要在根目錄中為您的應(yīng)用程序提供命名空間urls.py
path('your_url', include(('your_app.urls', 'your_app'), namespace="your_app"))
serializers.py
添加一個(gè)HyperlinkedIdentityField
class BookSerializer(serializers.HyperlinkedModelSerializer):
# add this
url = serializers.HyperlinkedIdentityField(view_name="your_app:book-detail")
class Meta:
model = Book
fields = ['id', 'url', 'title', 'subtitle', 'isbn']
urls.py(應(yīng)用程序)
添加basename
router = routers.DefaultRouter()
# add basename
router.register('books', BookListView, basename="books")
文檔https://www.django-rest-framework.org/tutorial/5-relationships-and-hyperlinked-apis/
添加回答
舉報(bào)