Django Rest框架 - 路由器注册无法正常工作

时间:2016-11-18 02:51:21

标签: python django rest django-rest-framework

我使用django rest框架生成api。在其中我有两个模型,相关的视图和序列化器。这些是beerspoursbeers api端点按预期工作,但每当我尝试访问pours端点时,我都会收到以下错误:

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

我不太确定我做错了什么,有人可以指出错误吗?

models.py

from __future__ import unicode_literals

from django.db import models
from django.utils import timezone


class Beers(models.Model):
    """
    Beers
    """
    # The unique id
    id = models.AutoField(primary_key=True)
    # Timestamps
    created_at = models.DateTimeField(default=None, null=True)
    updated_at = models.DateTimeField(default=None, null=True)
    tap = models.PositiveIntegerField(default=None, null=True)
    url = models.CharField(max_length=255, default=None, null=True)
    abv = models.DecimalField(default=None, null=True, decimal_places=2, max_digits=3)
    style = models.CharField(max_length=255, default=None, null=True)
    score = models.PositiveIntegerField(default=None, null=True)
    tap_date = models.PositiveIntegerField(default=None, null=True)
    size = models.PositiveIntegerField(default=None, null=True)

    def save(self, *args, **kwargs):
        """
        On save, update timestamps
        """
        # If the record does not currently exist in the database
        self.created_at = timezone.now()
        self.updated_at = timezone.now()

        return super(Beers, self).save(*args, **kwargs)

    class Meta:
        db_table = 'beers'
        index_together = [("tap", "tap_date")]


class Pours(models.Model):
    """
    Handles pours
    """
    id = models.AutoField(primary_key=True)
    tap = models.PositiveIntegerField()
    amount = models.DecimalField(decimal_places=3, max_digits=19)
    date = models.PositiveIntegerField()

    def save(self, *args, **kwargs):
        """
        On save, update timestamps
        """
        return super(Pours, self).save(*args, **kwargs)

    class Meta:
        db_table = 'pours'
        index_together = [("tap", "date")]

views.py

from rest_framework import viewsets
from rest_framework.renderers import JSONRenderer

from .serializers import *
from .models import *

import logging

logger = logging.getLogger(__name__)


class PoursView(viewsets.ModelViewSet):
    """
    Handles pours
    """
    permission_classes = ()
    throttle_classes = ()
    authentication_classes = ()

    renderer_classes = (JSONRenderer,)

    queryset = Pours.objects.all()
    serializer_class = PoursSerialzier


class BeersView(viewsets.ModelViewSet):
    """
    Handles beers
    """
    permission_classes = ()
    throttle_classes = ()
    authentication_classes = ()

    renderer_classes = (JSONRenderer,)

    queryset = Beers.objects.all()
    serializer_class = BeersSerialzier

serializers.py

from rest_framework import serializers
from .models import *


class BeersSerialzier(serializers.HyperlinkedModelSerializer):
    queryset = Beers.objects.all()

    class Meta:
        model = Beers


class PoursSerialzier(serializers.HyperlinkedModelSerializer):
    queryset = Pours.objects.all()

    class Meta:
        model = Pours

urls.py

from django.conf.urls import include, patterns, url
from rest_framework import routers

import views

router = routers.DefaultRouter()
router.register(r'beers', views.BeersView)
router.register(r'pours', views.PoursView)

urlpatterns = [
    url('^', include(router.urls)),
]

0 个答案:

没有答案