django – 使用Djoser和Rest Framework中的自定义字段注册用户

我正在尝试使用
Djoser的“注册”端点.它工作正常,但我需要更多的字段而不仅仅是“用户名”,“电子邮件”和“密码”.

我已经看过这个
question,我确实可以看到我想要的字段(在可浏览的API中).但是当我尝试发布它时,我收到了这个错误

ImproperlyConfigured at /account/register/ Could not resolve URL
for hyperlinked relationship using view name “user-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 django.db import models


class User(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    email = models.CharField(max_length=100, blank=False)
    name = models.CharField(max_length=100, blank=False)
    last_name = models.CharField(max_length=100, blank=False)
    birthday = models.CharField(max_length=15, blank=False)
    password = models.CharField(max_length=100, blank=False)

    class Meta:
        ordering = ('created',)

serializers.py

from rest_framework import serializers
from users.models import User


class UserSerializer(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = User
        fields = ('url', 'id', 'email', 'name', 'last_name', 'birthday', 'password')

我的views.py

from users.models import User
from users.serializers import UserSerializer
from rest_framework import viewsets


class UserViewSet(viewsets.ModelViewSet):
    queryset = User.objects.all()
    serializer_class = UserSerializer

在settings.py中我添加了:

DJOSER = {
    ...
    'SERIALIZERS': {
        'user_registration': 'users.serializers.UserSerializer',
    },
}

编辑

应用/ urls.py

from django.conf.urls import url, include
from users import views
from rest_framework.routers import DefaultRouter
from rest_framework.schemas import get_schema_view

# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'users', views.UserViewSet)

schema_view = get_schema_view(title='Pastebin API')

# The API URLs are now determined automatically by the router.
# Additionally, we include the login URLs for the browsable API.

urlpatterns = [
    url(r'^', include(router.urls)),
    url('^schema/$', schema_view),
    url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]

API / urls.py

from django.conf.urls import url, include
from django.contrib import admin
from rest_framework_jwt import views as jwt_views
from rest_framework import routers

router = routers.DefaultRouter()

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(router.urls)),
    url(r'^account/', include('djoser.urls')),
    url(r'^auth/login/', jwt_views.obtain_jwt_token, name='auth'),
]

有人有想法吗?

最佳答案 例外情况很明显:Django无法按用户详细信息名称解析URL.它试图这样做,因为你已经使用url字段从serializers.HyperlinkedModelSerializer创建了一个扩展的序列化程序,该字段应包含指向特定用户对象的链接.

我看到的问题是你的主urls.py不包括api urls,它们都是重复的,并且有点奇怪.我会按如下方式重写它(假设你也有用户应用程序):

Root urls.py:

from django.conf.urls import url, include
from django.contrib import admin

from rest_framework.schemas import get_schema_view
from rest_framework_jwt import views as jwt_views

schema_view = get_schema_view(title='Pastebin API')

urlpatterns = [
    url('^schema/$', schema_view),
    url(r'^admin/', admin.site.urls),
    url(r'^user/', include('user.urls')),
    url(r'^account/', include('djoser.urls')),
    # Not sure if you need both:
    url(r'^auth/login/', jwt_views.obtain_jwt_token, name='auth'),
    url(
        r'^api-auth/',
        include('rest_framework.urls', namespace='rest_framework')),
]

用户应用urls.py:

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

from .views import UserViewSet

router = DefaultRouter()
router.register(r'users', UserViewSet)

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

而且我不确定为什么你需要另一款名为api的应用程序.

如果它没有帮助,请尝试在namespacing the router上查看本文.

点赞