0
votes

I search a lot for this problem but no any solution work for me. I am get 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. when I want to access this resource through my API in django.

I am getting error like this:

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.

My model is ass follows:

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)

My 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')

My views:

@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

My urls

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'),
]

How can I resolve this issue?

2

2 Answers

0
votes

Since I can't comment:

Your class Meta classes have the wrong indentation. They should be indented inside of their appropriate classes. If it is just a transcription error then it should be fixed for the question.

0
votes

I had a similar situation involving HyperlinkedRelatedField on a OneToOneField that was a model's primary key.

Ended up needing to specify both the lookup_field and the lookup_url_kwarg arguments for HyperlinkedRelatedField.

Here is what this solution would look like applied to your serializer:

class TourSiteSerializer(serializers.HyperlinkedModelSerializer):
    place_info = serializers.HyperlinkedIdentityField(
        view_name='place-info',
        lookup_field='place_info_id',      # <-- note the `_id` suffix
        lookup_url_kwarg='place_info',
    )

    class Meta:
        model = TourSite
        fields = ('id', 'place_name','place_info', 'place_address', 'place_image')