Incorrectly configured attached resource using HyperlinkedModelSerializer

I chose the Django REST Framework and drf-nested-routers to create an API for products and ProductReports. Here are the relevant classes:

# models.py from django.db import models class Product(models.Model): created_at = models.DateTimeField(auto_now_add=True) class ProductReport(models.Model): user_name = models.CharField(max_length=256, blank=False) created_at = models.DateTimeField(auto_now_add=True) product = models.ForeignKey('Product', default=1) 

...

 # serializers.py from rest_framework import serializers from models import Product, ProductReport class ProductSerializer(serializers.HyperlinkedModelSerializer): class Meta: model = Product class ProductReportSerializer(serializers.HyperlinkedModelSerializer): product = ProductSerializer() class Meta: model = ProductReport 

...

 # views.py from django.shortcuts import render from rest_framework import viewsets, mixins from rest_framework.response import Response from models import Product, ProductReport from serializers import ProductSerializer, ProductReportSerializer class ProductViewSet(mixins.RetrieveModelMixin, mixins.ListModelMixin, viewsets.GenericViewSet): queryset = Product.objects.all() serializer_class = ProductSerializer class ProductReportViewSet(viewsets.ViewSet): queryset = ProductReport.objects.all() serializer_class = ProductReportSerializer def list(self, request, product_pk=None): queryset = self.queryset.filter(product=product_pk) serializer = ProductReportSerializer(queryset, many=True, context={'request': request}) return Response(serializer.data) def retrieve(self, request, pk=None, product_pk=None): queryset = self.queryset.get(pk=pk, product=product_pk) serializer = ProductReportSerializer(queryset) return Response(serializer.data) 

...

 # urls.py from django.conf.urls import patterns, include, url from django.contrib import admin from rest_framework import routers from rest_framework_nested.routers import SimpleRouter, NestedSimpleRouter from productreports import views router = routers.SimpleRouter() router.register(r'products', views.ProductViewSet) products_router = NestedSimpleRouter(router, r'products', lookup='product') products_router.register(r'productreports', views.ProductReportViewSet) urlpatterns = patterns('', url(r'^', include(router.urls)), url(r'^', include(products_router.urls)), url(r'^admin/', include(admin.site.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), ) 

When visiting http://localhost:8000/products/1/productreports/ , the following error is displayed:

Incorrectly configured in / products / 1 / productreports /
Unable to resolve URLs for hyperlinks using view name "productreport-detail". You may not have been able to include the linked model in your API, or you might have incorrectly configured the lookup_field attribute in this field.

There are a few things that I'm not sure about whether they are configured correctly:

  • product = models.ForeignKey('Product', default=1)
  • many=True in serializer = ProductReportSerializer(queryset, many=True, context={'request': request}) ... each product can have many product reports
  • ProductReportSerializer in general
  • lookup='product' in products_router = NestedSimpleRouter(router, r'products', lookup='product')

Update

I noticed that an error only occurs when there is at least one product associated with it for a product. . If the productreports table does not contain an entry for a specific product, then the API response is excellent.

When I check the serializer in ProductReportViewSet#list , I get:

 ProductReportSerializer([ <ProductReport: ProductReport object>, <ProductReport: ProductReport object>, <ProductReport: ProductReport object>, <ProductReport: ProductReport object>, <ProductReport: ProductReport object>, <ProductReport: ProductReport object>, '...(remaining elements truncated)...'], context={'request': <rest_framework.request.Request object>}, many=True): url = HyperlinkedIdentityField(view_name='productreport-detail') product = ProductSerializer(): url = HyperlinkedIdentityField(view_name='product-detail') created_at = DateTimeField(read_only=True) user_name = CharField(max_length=256) created_at = DateTimeField(read_only=True) 
+3
django django-rest-framework nested-resources
Mar 18 '15 at 15:55
source share

No one has answered this question yet.

See similar questions:

92
Django Rest Framework - Unable to resolve URLs for hyperlinks using view name "user-detail"
61
create or update django-rest-framework 3.0 in a nested serializer
5
Django-rest-framework nested urls with drf nested routers

or similar:

122
Django REST Structure: A Non-Model Serializer
6
How to get the two best products for each type of product?
5
django - inlineformset_factory with several foreign keys
3
Django-Rest-Framework - how to serialize a request from an unrelated model as a nested serializer
one
django models request get id error Number matching request does not exist
one
input_formats in django admin does not affect
one
Create a new model that has all the fields of the existing model
one
Product catalog: filter by parameters
0
Serializer as field not appearing in json
0
there is no error in the '_meta' attribute when trying to run a method to remove a string from a URL called .get ()



All Articles