1

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

Serializers.py

post_detail_url = HyperlinkedRelatedField(
view_name="posts-api:detail",
read_only=True,
lookup_field='slug'
)


class PostListSerializer(ModelSerializer):
class Meta:
    url = post_detail_url
    fields = (
        'id',
        'url',
        'user',
        'title',
        'content',
        'created_at',
        'updated_at',
    )
    model = Post

Urls.py

urlpatterns = [
url(r'^$', PostListAPIView.as_view(), name='list'),
url(r'^create/$', PostCreateAPIView.as_view(), name='create'),
url(r'^(?P<slug>[\w-]+)/$', PostDetailAPIView.as_view(), name='detail'),
url(r'^(?P<slug>[\w-]+)/edit/$', PostUpdateAPIView.as_view(), name='update'),
url(r'^(?P<slug>[\w-]+)/delete/$', PostDeleteAPIView.as_view(), name='delete'),  
]

Post model

class Post(models.Model):
user                = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, default=1)
title               = models.CharField(max_length=200)
content             = models.TextField()
posted              = models.DateField(db_index=True, auto_now_add=True)
slug                = models.SlugField(max_length=100, unique=True, blank=True)
created_at          = models.DateTimeField(auto_now_add=True)
updated_at          = models.DateTimeField(auto_now=True)

def __str__(self):
    return self.title
TrynaLearnMath
  • 73
  • 1
  • 3
  • 10

2 Answers2

0

Serializer's field should be defined as serializer attribute, not meta attribute:

class PostListSerializer(ModelSerializer):
    url = HyperlinkedRelatedField(
        view_name="posts-api:detail",
        read_only=True,
        lookup_field='slug'
    )

    class Meta:
        ...

Also if you need to get url of post instead of url of some related object you should use HyperlinkedIdentityField:

class PostListSerializer(ModelSerializer):
    url = HyperlinkedIdentityField(
        view_name="posts-api:detail",
        read_only=True,
        lookup_field='slug'
    )
    class Meta:
        fields = (
            'id',
            'url',
            'user',
            'title',
            'content',
            'created_at',
            'updated_at',
        )
        model = Post
neverwalkaloner
  • 46,181
  • 7
  • 92
  • 100
  • That worked, thank you! Why does it have to be defined as a serializer attribute? What if i wanted to declare the same url in another SerializerModel, that means I would have to re-write the same code in another serializer? – TrynaLearnMath Jun 12 '18 at 15:22
  • @MichaelTeixeira serializer will not able to see field declared on `meta` level. To reduce copy-paste you can wrilte simple mixin class with url field defined in it, and use it as base class for your serializers: `class PostListSerializer(UrlMixin, ModelSerializer):`. Check example here: https://stackoverflow.com/questions/28747487/mixin-common-fields-between-serializers-in-django-rest-framework – neverwalkaloner Jun 12 '18 at 15:29
0

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

url = HyperlinkedIdentityField(view_name="posts-api:detail" ,read_only=True, lookup_field ='slug')

class Meta:
    model = Post
    fields = [
    'url',
    'user',
    'id',
    'title',
    'slug',
    'content',
    ]
Palani P
  • 81
  • 1
  • 1
  • 6