Django Rest Framework - Could not resolve URL for hyperlinked relationship using view name “field-detail”

2,212 views
Skip to first unread message

Bernardo Garcia

unread,
Nov 11, 2016, 12:06:53 PM11/11/16
to Django REST framework

I know that this inconvenient is very presented, may be that I need learn more about of serializer relationships

I have the following model:



class Field(models.Model):

    FIELD_TYPE_NATURE = 'Grama natural'
    FIELD_TYPE_ARTIFICIAL = 'Grama sintetica'

    FIELD_TYPE_CHOICES = (
        (FIELD_TYPE_NATURE, u'Grama natural'),
        (FIELD_TYPE_ARTIFICIAL, u'Grama sintetica'),
    )

    MODALITY_11 = 'Fútbol 11'
    MODALITY_8 = 'Fútbol 8'


    MODALITY_CHOICES = (
        (MODALITY_11, u'Fútbol 11'),
        (MODALITY_8, u'Fútbol 8'),
    )

    name = models.CharField(
        max_length=150,
        unique=True,
        db_index=True,
        primary_key=True,
        )

    field_type = models.CharField(
        choices=FIELD_TYPE_CHOICES,
        default=False,
        blank=False,
        max_length=20,
        verbose_name=('Tipo de material/grama de la cancha')
    )

    modality = models.CharField(
        max_length=40,
        blank=False,
        verbose_name='Modalidad'
    )
    photo = models.ImageField(upload_to='fields', blank=True, null=True)
    location = models.CharField(max_length=150, blank=False)

    def __str__(self):
        return '%s %s %s' % (self.name, self.field_type, self.location)



My serializer is the following:

class FieldSerializer(serializers.HyperlinkedModelSerializer):
    #url = serializers.HyperlinkedIdentityField(view_name='field-detail',)

    class Meta:
        model = Field
        fields = ('url', 'name','field_type','modality','photo','location')


My viewset is:

class FieldViewSet(viewsets.ModelViewSet):
    queryset = Field.objects.all()
    serializer_class = FieldSerializer


This is my router:

router = routers.DefaultRouter()
router.register(r'fields', FieldViewSet)


And my url:

...
url(r'^api/', include(router.urls)),
...



When I go to the http://localhost:8000/api/fields/ url I get the following message:


File "/home/bgarcial/.virtualenvs/fuupbol2/lib/python3.5/site-packages/rest_framework/relations.py", line 386, in to_representation
    raise ImproperlyConfigured(msg % self.view_name)
django.core.exceptions.ImproperlyConfigured: Could not resolve URL for **hyperlinked relationship using view name "field-detail". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.**
[11/Nov/2016 16:39:53] "GET /api/fields/ HTTP/1.1" 500 187477



When I use HyperlinkedIdentityField in my FieldSerializer class:

class FieldSerializer(serializers.HyperlinkedModelSerializer):
    url = serializers.HyperlinkedIdentityField(view_name='field-detail',)

    class Meta:
        model = Field
        fields = ('url', 'name','field_type','modality','photo','location')



I follow getting the same error. Althought when I go to the url http://localhost:8000/api/fields/ I want get is a list of my objects, then is possible that I should put:

url = serializers.HyperlinkedIdentityField(view_name='field-list',)

?

I use HyperlinkedIdentityField according to:

This field can be applied as an identity relationship, such as the 'url' field on a HyperlinkedModelSerializer. It can also be used for an attribute on the object.


I put the field-list in my view_name attribute and I get the error related

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



I don't understand the situations when I should use view_name attribute in relation to if I wnt get a list objects, a object detail and so ... although here explain something about it.

When I should use HyperlinkedModelSerializer and ModelSerializer

Xavier Ordoquy

unread,
Nov 14, 2016, 5:14:55 AM11/14/16
to Django REST framework
Hi,

You declared your view as "fields" in the routers, so you should use "fields-detail" as the view name for the hyperlink.

Regards,
Xavier,
Linovia.

Bernardo Garcia

unread,
Nov 14, 2016, 12:57:52 PM11/14/16
to Django REST framework
I understand. Thanks Xavier This works for me :)
Reply all
Reply to author
Forward
0 new messages