1
votes

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
2
You need to get url of current post in json response?neverwalkaloner
I have react setup as my front end that will be displaying the data. As of now I'm just setting up the backend rest-api to view the json response. So yes the current post will be shown as json response.TrynaLearnMath
Can you add post model?neverwalkaloner
Added the post model.TrynaLearnMath

2 Answers

0
votes

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
0
votes

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