无法使用视图名称解析超链接关系的URL" shop-detail"

时间:2018-03-26 11:52:03

标签: django django-rest-framework

即将收到错误无法使用视图名称解析超链接关系的网址" shop-detail"

在文件 shop / api / urls.py

router = routers.DefaultRouter()
router.register('shops', ShopView, base_name='shop')
urlpatterns = [
path('', include(router.urls)),]

文件商店/ api / serializers.py

class ShopSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
    view_name="shop-detail",
    lookup_field='id',)
class Meta:
    model = Shop
    fields = [
        'id',
        'url',
        'name',
    ]
那边有什么不对?如果我不添加' url'字段。

修改

添加了视图代码

class ShopView(viewsets.ModelViewSet):
lookup_field = 'id'
queryset = Shop.objects.all()
serializer_class = ShopSerializer

3 个答案:

答案 0 :(得分:0)

您正在注册shops,因此当您使用shops-detail时,详细信息视图的默认名称将为shop-detail。注意缺少的" S"

通过以下方式解决此问题:

class ShopSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(
        view_name="shops-detail",
        lookup_field='id',)

答案 1 :(得分:0)

默认情况下,在https://android--code.blogspot.in/2015/08/android-spinner-get-selected-item-text.html中,序列化程序将包含 url 字段,而不是主键字段。请参阅HyperlinkedModelSerializer

class ShopSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = Shop
        fields = [
          'id',
          'url',
         'name',
        ]

答案 2 :(得分:0)

发现问题,不需要查看view_field。

I2C_MESSAGE_STATUS status;
uint8_t pdata_write[2] = { 0, 0x10 }; // 0x10 into 'seconds' register
I2C2_MasterWrite(pdata_write, 1, 0b1101000, &status);
while (status == I2C_MESSAGE_PENDING); // wait for status to to change
if (status == I2C_MESSAGE_COMPLETE) {
    // done!
} else {
    // error
}