Django-rest-framework hyperlinkedmodelserializer和foreignkey cities-light

时间:2014-11-24 17:47:32

标签: python django django-rest-framework

我刚刚使用DRF为我的项目安装了django-cities-light,我无法让它工作。

我的用户模型将city和coutry字段定义为foreignkey,这是我在尝试获取用户时获得的内容:

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

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

在Rest Framework 3中使用django-cities-light,您需要使用他们提供的view_name:

<强>城市型光API- {模型} -detail

class FooSerializer(HyperlinkedModelSerializer):
    url = relations.HyperlinkedIdentityField(view_name="foo-detail")
    city = relations.HyperlinkedRelatedField(
        view_name="cities-light-api-city-detail", queryset=City.objects.all(),
    )

    class Meta:
        model = Foo
        read_only_fields = ('id',)