我经常搜索这个问题,但没有任何解决方案适合我。我得到无法使用视图名称" place-info"解析超链接关系的URL。您可能未能在API中包含相关模型,或者未能在此字段中错误配置lookup_field
属性。当我想通过django中的API访问此资源时。
我收到这样的错误:
Could not resolve URL for hyperlinked relationship using view name "place-info". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
我的模型是屁股:
class TourSite(models.Model):
"""Destination place"""
place_name = models.CharField(max_length=100, blank=False, default='')
place_address = models.CharField(max_length=200, blank=False, default='')
place_image = models.ImageField(upload_to="TourSite", blank=True)
def get_place_name(self):
return self.place_name
def get_place_address(self):
return self.place_address
def save(self, *args, **kwargs):
super(TourSite, self).save(*args, **kwargs)
def __str__(self):
return self.place_name
class Info(models.Model):
"""Stores information about the place."""
place_name = models.OneToOneField(
TourSite,
on_delete = models.CASCADE,
primary_key = True,
)
famous_for = models.CharField(max_length=200, blank=True, default='')
brief_info = models.TextField(blank=True, default='')
def get_famous_for(self):
return self.famous_for
def get_brief_info(self):
return self.brief_info
def get_place_name(self):
return self.place_name
def save(self, *args, **kwargs):
super(Info, self).save(*args, **kwargs)
def __str__(self):
return "%s is famous for %s"% (self.place_name, self.famous_for)
我的Serailizer:
class InfoSerializer(serializers.ModelSerializer):
class Meta:
model = Info
fields = '__all__'
class TourSiteSerializer(serializers.HyperlinkedModelSerializer):
place_info = serializers.HyperlinkedIdentityField(view_name='place-info')
class Meta:
model = TourSite
fields = ('id', 'place_name','place_info', 'place_address', 'place_image')
我的观点:
@api_view(['GET'])
def api_root(request):
return Response({
'places': reverse('places-list', request=request),
})
class TourSiteList(DefaultsMixin, generics.ListAPIView):
queryset = TourSite.objects.all()
search_fields = ("place_name", "place_address", )
serializer_class = TourSiteSerializer
class TourSiteDetail(DefaultsMixin, generics.RetrieveUpdateDestroyAPIView):
queryset = TourSite.objects.all()
serializer_class = TourSiteSerializer
class Info(generics.ListAPIView):
queryset = Info.objects.all()
serializer_class = InfoSerializer
我的网址
urlpatterns = [
url(r'^$', views.api_root),
url(r'^places/$', views.TourSiteList.as_view(), name="places-list"),
url(r'^places/(?P<pk>[0-9]+)/$', views.TourSiteDetail.as_view(), name="places-detail"),
url(r'^info/$', views.Info.as_view(), name='place-info'),
]
如何解决此问题?
答案 0 :(得分:0)
因为我无法发表评论:
您的class Meta
班级有错误的缩进。它们应该缩进其适当的类中。如果它只是一个转录错误,那么应该修复这个问题。
答案 1 :(得分:0)
我在[{1}}上遇到类似 if(selectedValue.getSelectedIndex() == 0)
{
displayRecords.setText("You have selected index 0");
for(int i=0; i<vList.size(); i++) {
String tokenizerValue = vList.get(i).toString();
System.out.println(tokenizerValue);
textArea.setText(tokenizerValue);
}
}
的情况,这是模型的主键。
结束时需要为HyperlinkedRelatedField
指定OneToOneField
和lookup_field
个参数。
以下是此解决方案应用于序列化程序的内容:
lookup_url_kwarg