我試圖使用Djoser中的「註冊」端點。它工作正常,但我需要更多的字段,而不僅僅是「用戶名」,「電子郵件」和「密碼」。 我見過這個question,事實上我可以看到我想要的字段(在可瀏覽的API中)。但是,當我嘗試後,我得到這個錯誤用Djoser和Rest框架中的自定義字段註冊用戶
ImproperlyConfigured在/帳號/註冊/無法解析使用視圖名稱「用戶細節」超鏈接的URL的關係 。您可能在 未能在您的API中包含相關模型,或錯誤地 在此字段上配置了
lookup_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'),
]
是否有人有想法?
嘗試將'User'模型重命名爲'ExtendedUser',並在其字段中包含與django.contrib.aut.models.User模型的OneToOne關係。如果運作良好,我會解釋原因。 – Nevertheless
另外,我沒有在序列化程序中看到HyperlinkedIdentityField ... – Nevertheless
嗨,孩子,謝謝。 我是Django和Rest框架中的新成員。 '''你是什麼意思與'''包括它的領域一個OneToOne與django.contrib.auth.models.User model'''的關係? 我看了[這裏](https://docs.djangoproject.com/en/1.10/ref/contrib/auth/),但我不知道該怎麼做。 –