0
votes

Adding an entry to color works but adding one to temperature trows:

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

I tried several options using namespaces and things like that. I have only defined the temperature model. Nothing else from it.

urls.py (project)

...
router = routers.DefaultRouter()

urlpatterns = [
    ...
    path('', include('light_analysis.urls')),
    path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

urls.py (app)

from django.urls import include, path
from . import views
from something_web.urls import router

router.register(r'light', views.LightViewSet)
urlpatterns = [
    path('', include(router.urls)),
]

models.py

...
class Light(models.Model):
    color_id              = models.ForeignKey(Color, on_delete=models.CASCADE)
    temperature           = models.CharField(max_length=63)
    temperature_id        = models.ForeignKey(Temperature, on_delete=models.CASCADE)

views.py

from rest_framework import viewsets
from common.models import Light
from .serializers import LightSerializer

class LightViewSet(viewsets.ModelViewSet):
    queryset = Light.objects.all()
    serializer_class = LightSerializer

serializers.py

from rest_framework import serializers
from common.models import Light

class LightSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = Light
        fields = '__all__'
1

1 Answers

0
votes

I was missing this in the Serializer:

temperature_id = serializers.PrimaryKeyRelatedField(queryset=Temperature.objects.all(), many=False)

and then call the field directly in the meta:

fields = ('temperature_id',)