我花了几个小时尝试调试问题。我还无法理解如何设置HyperlinkedModelSerializer。
能否请您指出我做错了什么?此外,除了官方文档外,是否还有任何其他有关更好理解超链接关系的好教程? 谢谢!
错误
在/ settings /中配置不正确无法解析URL 使用视图名称“设置详细信息”的超链接关系。你可以 未能将相关模型包含在您的API中,或不正确 在此字段上配置了
lookup_field
属性。...
在处理上述异常期间(“设置”为反向 详细信息”和关键字参数“ {'settings_id': 找不到UUID('24aee4eb-8f71-4336-8c36-a96c9e4447c9')}。 1个图案 尝试过:['settings // $']),发生了另一个异常:
models.py
class Settings(models.Model):
# General
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
createdAt = models.DateTimeField(auto_now_add=True)
updatedAt = models.DateTimeField(auto_now=True)
# Token
publicToken = models.CharField(max_length=100)
secretToken = models.CharField(max_length=100)
serializers.py
class SettingsListSerializer(serializers.HyperlinkedModelSerializer):
url = serializers.HyperlinkedIdentityField(
view_name = "Settings Detail",
lookup_field='id',
lookup_url_kwarg='settings_id'
)
class Meta:
model = Settings
exclude = ('id', 'createdAt', 'updatedAt',)
class SettingsDetailSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Settings
exclude = ('id','createdAt', 'updatedAt',)
urls.py
from django.conf.urls import url
from app import views
urlpatterns = [
url(r'^$', views.HomeView.as_view(), name=views.HomeView.name),
url(r'^settings/$', views.SettingsListView.as_view(), name=views.SettingsListView.name),
url(r'^settings/<uuid:settings_id>/$', views.SettingsDetailView.as_view(), name=views.SettingsDetailView.name),
]
views.py
class SettingsListView(generics.ListCreateAPIView):
queryset = Settings.objects.all()
serializer_class = SettingsListSerializer
permission_classes = (IsAuthenticated,)
name = "Settings List"
class SettingsDetailView(generics.RetrieveUpdateDestroyAPIView):
serializer_class = SettingsDetailSerializer
permission_classes = (IsAuthenticated,)
name = "Settings Detail"
def get_queryset(self):
id = self.kwargs.get(self.lookup_url_kwarg)
setting = Settings.objects.filter(id=id)
return setting