使用 Auth、用户模型、视图、序列化程序的 Django API OneToOne 关系
Posted
技术标签:
【中文标题】使用 Auth、用户模型、视图、序列化程序的 Django API OneToOne 关系【英文标题】:Django API OneToOne relationship using Auth, User model, Views, Serializers 【发布时间】:2019-09-11 07:25:24 【问题描述】:我正在尝试将 HighScore 模型连接到我的用户模型。这样我可以为每个用户保存一个 HighScore 实例。无论我做什么,都会收到 500 内部服务器错误。
我尝试使用 AbstractUser 创建自定义用户模型。我尝试使用 settings.AUTH_USER_MODEL 设置 OneToOne,并且我尝试执行 User = get_user_model(),但都返回 500 内部服务器错误。
# models.py
from django.conf import settings
from django.db import models
# Create your models here.
class HighScore(models.Model):
# user = models.OneToOneField(
# settings.AUTH_USER_MODEL,
# on_delete=models.CASCADE,
# primary_key=True,
# )
value = models.IntegerField(default=0)
def __str__(self):
return "".format(self.value)
# urls.py
from django.urls import path
from .views import ListHighScoresView, CreateHighScoresView, HighScoresDetailView, LoginView, RegisterUsersView
urlpatterns = [
path("highscores/", ListHighScoresView.as_view(), name="high-scores-all"),
path("highscores/create/", CreateHighScoresView.as_view(), name="high-scores-create"),
path("highscores/<int:pk>/", HighScoresDetailView.as_view(), name="high-scores-detail"),
path("auth/login/", LoginView.as_view(), name="auth-login"),
path("auth/register/", RegisterUsersView.as_view(), name="auth-register"),
]
# serializers.py
from rest_framework import serializers
from .models import HighScore
from django.contrib.auth.models import User
class HighScoreSerializer(serializers.ModelSerializer):
class Meta:
model = HighScore
fields = ("id", "value")
def update(self, instance, validated_data):
instance.value = validated_data.get("value", instance.value)
# instance.user = validated_data.get("user", instance.user)
instance.save()
return instance
class TokenSerializer(serializers.Serializer):
token = serializers.CharField(max_length=255)
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = ("username", "email")
# migration 0001_initial.py
# Generated by Django 2.0.3 on 2019-09-11 07:55
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
initial = True
dependencies = [
('auth', '0009_alter_user_last_name_max_length'),
]
operations = [
migrations.CreateModel(
name='HighScore',
fields=[
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
('value', models.IntegerField(default=0)),
],
),
]
# views.py
from django.shortcuts import render
from django.contrib.auth.models import User
from django.contrib.auth import authenticate, login
from rest_framework_jwt.settings import api_settings
from rest_framework.response import Response
from rest_framework.generics import GenericAPIView, RetrieveUpdateDestroyAPIView, ListAPIView
from rest_framework.views import APIView
from rest_framework import permissions, status
from .decorators import validate_request_data
from .models import HighScore
from .serializers import HighScoreSerializer, TokenSerializer, UserSerializer
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
class CreateHighScoresView(GenericAPIView):
""" GET and POST highscores/ """
queryset = HighScore.objects.all()
serializer_class = HighScoreSerializer
permission_classes = (permissions.IsAuthenticated,)
@validate_request_data
def post(self, request, *args, **kwargs):
new_high_score = HighScore.objects.create(
# user=request.user,
value=request.data["value"],
)
return Response(
data=HighScoreSerializer(new_high_score).data,
status=status.HTTP_201_CREATED
)
class HighScoresDetailView(RetrieveUpdateDestroyAPIView):
""" GET, PUT, DELETE highscores/:id/ """
queryset = HighScore.objects.all()
serializer_class = HighScoreSerializer
def get(self, request, *args, **kwargs):
try:
new_high_score = self.queryset.get(pk=kwargs["pk"])
return Response(HighScoreSerializer(new_high_score).data)
except HighScore.DoesNotExist:
return Response(
data=
"message": "Yo, that High Score with id: does not exist".format(kwargs["pk"])
,
status=status.HTTP_404_NOT_FOUND
)
@validate_request_data
def put(self, request, *args, **kwargs):
try:
new_high_score = self.queryset.get(pk=kwargs["pk"])
serializer = HighScoreSerializer()
updated_high_score = serializer.update(new_high_score, request.data)
return Response(HighScoreSerializer(updated_high_score).data)
except HighScore.DoesNotExist:
return Response(
data=
"message": "Yo, that High Score with id: does not exist".format(kwargs["pk"])
,
status=status.HTTP_404_NOT_FOUND
)
def delete(self, request, *args, **kwargs):
try:
new_high_score = self.queryset.get(pk=kwargs["pk"])
new_high_score.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
except HighScore.DoesNotExist:
return Response(
data=
"message": "Yo, that High Score with id: does not exist".format(kwargs["pk"])
,
status=status.HTTP_404_NOT_FOUND
)
class ListHighScoresView(ListAPIView):
""" Provides a GET method handler. """
queryset = HighScore.objects.all()
serializer_class = HighScoreSerializer
permission_classes = (permissions.IsAuthenticated,)
class LoginView(APIView):
""" POST auth/login/ """
# This permission class will overide the global permission class setting
permission_classes = (permissions.AllowAny,)
queryset = User.objects.all()
def post(self, request, *args, **kwargs):
username = request.data.get("username", "")
password = request.data.get("password", "")
user = authenticate(request, username=username, password=password)
if user is not None:
# Login saves the user's ID in the session, using Django's session framework.
login(request, user)
serializer = TokenSerializer(
data=
# Using drf jwt utility functions to generate a token
"token": jwt_encode_handler(
jwt_payload_handler(user)
)
)
serializer.is_valid()
username = user.username
email = user.email
token = serializer.data["token"]
return Response(
data=
"username": username,
"email": email,
"token": token
,
status=status.HTTP_201_CREATED
)
return Response(status=status.HTTP_401_UNAUTHORIZED)
class RegisterUsersView(APIView):
""" Post auth/register/ """
permission_classes = (permissions.AllowAny,)
def post(self, request, *args, **kwargs):
username = request.data.get("username", "")
password = request.data.get("password", "")
email = request.data.get("email", "")
if not username and not password and not email:
return Response(
data=
"message": "Yo, gotta have a username, password, and email to register."
,
status=status.HTTP_400_BAD_REQUEST
)
new_user = User.objects.create_user(
username=username, password=password, email=email
)
user = authenticate(request, username=username, password=password)
login(request, user)
serializer = TokenSerializer(
data=
# Using drf jwt utility functions to generate a token.
"token": jwt_encode_handler(
jwt_payload_handler(user)
)
)
serializer.is_valid()
token = serializer.data["token"]
return Response(
data=
"username": username,
"email": email,
"token": token
,
status=status.HTTP_201_CREATED
)
#settings.py
import os
import datetime
import django_heroku
from django.conf import settings
from django.http import HttpResponseRedirect
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEBUG = False
ALLOWED_HOSTS = [
'127.0.0.1',
'.herokuapp.com'
]
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders',
'rest_framework',
'edjudicator_game',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
]
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
ROOT_URLCONF = 'edjudicator_api.urls'
TEMPLATES = [
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS':
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
,
,
]
WSGI_APPLICATION = 'edjudicator_api.wsgi.application'
DATABASES =
'default':
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'c',
'USER': 'a',
'PASSWORD': 'b',
'HOST': 'localhost',
'PORT': '5432',
,
AUTH_PASSWORD_VALIDATORS = [
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
,
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
,
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
,
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
,
]
LOGIN_REDIRECT_URL = '/'
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'America/Denver'
USE_I18N = True
USE_L10N = True
USE_TZ = True
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
REST_FRAMEWORK =
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.URLPathVersioning',
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
'rest_framework.authentication.BasicAuthentication',
],
'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly',
],
JWT_AUTH =
'JWT_ENCODE_HANDLER':
'rest_framework_jwt.utils.jwt_encode_handler',
'JWT_DECODE_HANDLER':
'rest_framework_jwt.utils.jwt_decode_handler',
'JWT_PAYLOAD_HANDLER':
'rest_framework_jwt.utils.jwt_payload_handler',
'JWT_PAYLOAD_GET_USER_ID_HANDLER':
'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler',
'JWT_RESPONSE_PAYLOAD_HANDLER':
'rest_framework_jwt.utils.jwt_response_payload_handler',
'JWT_SECRET_KEY': SECRET_KEY,
'JWT_GET_USER_SECRET_KEY': None,
'JWT_PUBLIC_KEY': None,
'JWT_PRIVATE_KEY': None,
'JWT_ALGORITHM': 'HS256',
'JWT_VERIFY': True,
'JWT_VERIFY_EXPIRATION': True,
'JWT_LEEWAY': 0,
'JWT_EXPIRATION_DELTA': datetime.timedelta(hours=4),
'JWT_AUDIENCE': None,
'JWT_ISSUER': None,
'JWT_ALLOW_REFRESH': False,
'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),
'JWT_AUTH_HEADER_PREFIX': 'Bearer',
'JWT_AUTH_COOKIE': None,
django_heroku.settings(locals())
class SSLMiddleware(object):
def process_request(self, request):
if not any([settings.DEBUG, request.is_secure(), request.META.get("HTTP_X_FORWARDED_PROTO", "") == 'https']):
url = request.build_absolute_uri(request.get_full_path())
secure_url = url.replace("http://", "https://")
return HttpResponseRedirect(secure_url)
# error stack
2019-09-11T07:52:36.173427+00:00 heroku[router]: at=info method=POST path="/api/v1/auth/login/" host=edjudicatorback.herokuapp.com request_id=dcdade1d-4581-4914-88bb-a90d40bce845 fwd="4.34.47.42" dyno=web.1 connect=1ms service=436ms status=201 bytes=883 protocol=https
2019-09-11T07:52:36.176354+00:00 app[web.1]: 10.143.94.115 - - [11/Sep/2019:01:52:36 -0600] "POST /api/v1/auth/login/ HTTP/1.1" 201 257 "https://edjudicator.herokuapp.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
2019-09-11T07:52:42.632366+00:00 heroku[router]: at=info method=GET path="/api/v1/highscores/" host=edjudicatorback.herokuapp.com request_id=2ae69eaf-3a2f-4818-a811-3bfcd4a6029b fwd="4.34.47.42" dyno=web.1 connect=1ms service=123ms status=500 bytes=18715 protocol=https
2019-09-11T07:52:42.526114+00:00 app[web.1]: Internal Server Error: /api/v1/highscores/
2019-09-11T07:52:42.526128+00:00 app[web.1]: Traceback (most recent call last):
2019-09-11T07:52:42.526139+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
2019-09-11T07:52:42.526142+00:00 app[web.1]: return self.cursor.execute(sql, params)
2019-09-11T07:52:42.526144+00:00 app[web.1]: psycopg2.errors.UndefinedColumn: column edjudicator_game_highscore.user_id does not exist
2019-09-11T07:52:42.526148+00:00 app[web.1]: LINE 1: SELECT "edjudicator_game_highscore"."user_id", "edjudicator_...
2019-09-11T07:52:42.526151+00:00 app[web.1]: ^
2019-09-11T07:52:42.526153+00:00 app[web.1]:
2019-09-11T07:52:42.526155+00:00 app[web.1]:
2019-09-11T07:52:42.526161+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2019-09-11T07:52:42.526163+00:00 app[web.1]:
2019-09-11T07:52:42.526166+00:00 app[web.1]: Traceback (most recent call last):
2019-09-11T07:52:42.526168+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
2019-09-11T07:52:42.526170+00:00 app[web.1]: response = get_response(request)
2019-09-11T07:52:42.526178+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
2019-09-11T07:52:42.526180+00:00 app[web.1]: response = self.process_exception_by_middleware(e, request)
2019-09-11T07:52:42.526183+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
2019-09-11T07:52:42.526185+00:00 app[web.1]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
2019-09-11T07:52:42.526187+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
2019-09-11T07:52:42.526190+00:00 app[web.1]: return view_func(*args, **kwargs)
2019-09-11T07:52:42.526192+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/views/generic/base.py", line 69, in view
2019-09-11T07:52:42.526194+00:00 app[web.1]: return self.dispatch(request, *args, **kwargs)
2019-09-11T07:52:42.526197+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/views.py", line 505, in dispatch
2019-09-11T07:52:42.526199+00:00 app[web.1]: response = self.handle_exception(exc)
2019-09-11T07:52:42.526201+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/views.py", line 465, in handle_exception
2019-09-11T07:52:42.526203+00:00 app[web.1]: self.raise_uncaught_exception(exc)
2019-09-11T07:52:42.526205+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/views.py", line 476, in raise_uncaught_exception
2019-09-11T07:52:42.526207+00:00 app[web.1]: raise exc
2019-09-11T07:52:42.526210+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/views.py", line 502, in dispatch
2019-09-11T07:52:42.526212+00:00 app[web.1]: response = handler(request, *args, **kwargs)
2019-09-11T07:52:42.526214+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/generics.py", line 199, in get
2019-09-11T07:52:42.526216+00:00 app[web.1]: return self.list(request, *args, **kwargs)
2019-09-11T07:52:42.526217+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/mixins.py", line 46, in list
2019-09-11T07:52:42.526219+00:00 app[web.1]: return Response(serializer.data)
2019-09-11T07:52:42.526220+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/serializers.py", line 757, in data
2019-09-11T07:52:42.526222+00:00 app[web.1]: ret = super().data
2019-09-11T07:52:42.526224+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/serializers.py", line 261, in data
2019-09-11T07:52:42.526226+00:00 app[web.1]: self._data = self.to_representation(self.instance)
2019-09-11T07:52:42.526227+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/rest_framework/serializers.py", line 675, in to_representation
2019-09-11T07:52:42.526229+00:00 app[web.1]: self.child.to_representation(item) for item in iterable
2019-09-11T07:52:42.526231+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 272, in __iter__
2019-09-11T07:52:42.526232+00:00 app[web.1]: self._fetch_all()
2019-09-11T07:52:42.526234+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1179, in _fetch_all
2019-09-11T07:52:42.526235+00:00 app[web.1]: self._result_cache = list(self._iterable_class(self))
2019-09-11T07:52:42.526237+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 53, in __iter__
2019-09-11T07:52:42.526239+00:00 app[web.1]: results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
2019-09-11T07:52:42.526240+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1066, in execute_sql
2019-09-11T07:52:42.526242+00:00 app[web.1]: cursor.execute(sql, params)
2019-09-11T07:52:42.526243+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 100, in execute
2019-09-11T07:52:42.526245+00:00 app[web.1]: return super().execute(sql, params)
2019-09-11T07:52:42.526246+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 68, in execute
2019-09-11T07:52:42.526248+00:00 app[web.1]: return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
2019-09-11T07:52:42.526250+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
2019-09-11T07:52:42.526251+00:00 app[web.1]: return executor(sql, params, many, context)
2019-09-11T07:52:42.526253+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
2019-09-11T07:52:42.526254+00:00 app[web.1]: return self.cursor.execute(sql, params)
2019-09-11T07:52:42.526256+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
2019-09-11T07:52:42.526265+00:00 app[web.1]: raise dj_exc_value.with_traceback(traceback) from exc_value
2019-09-11T07:52:42.526267+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute
2019-09-11T07:52:42.526268+00:00 app[web.1]: return self.cursor.execute(sql, params)
2019-09-11T07:52:42.526270+00:00 app[web.1]: django.db.utils.ProgrammingError: column edjudicator_game_highscore.user_id does not exist
2019-09-11T07:52:42.526271+00:00 app[web.1]: LINE 1: SELECT "edjudicator_game_highscore"."user_id", "edjudicator_...
2019-09-11T07:52:42.526273+00:00 app[web.1]: ^
2019-09-11T07:52:42.526325+00:00 app[web.1]:
2019-09-11T07:52:42.633754+00:00 app[web.1]: 10.143.94.115 - - [11/Sep/2019:01:52:42 -0600] "GET /api/v1/highscores/ HTTP/1.1" 500 18385 "https://edjudicator.herokuapp.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36"
我要做的就是让 HighScore 模型与用户建立 OneToOne 关系。这样,当用户登录时,我可以从我的前端发送 POST 请求,为该用户创建一个 HighScore。任何和所有的帮助都将非常感激!!!
编辑:我需要重置我的数据库。使用drop database <database name>
删除了我的 psql 数据库,然后使用create database <db name> with owner <user_name>
还做了heroku restart
加上heroku pg:reset DATABASE
并在两者上运行迁移以使它们工作。谢谢大家!
【问题讨论】:
您可以发布您使用 500 获得的堆栈跟踪吗? 添加到所有代码的末尾。感谢您的观看! 您应该将 DEBUG 设置为 True 并让 Django 告诉您实际的错误是什么。 完成,并更新了错误堆栈。谢谢。 【参考方案1】:您无需为此创建前端,只需使用post save signal
即可:
class HighScore(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
primary_key=True,
)
value = models.IntegerField(default=0)
@receiver(post_save, sender=User)
def create_user_score(sender, instance, created, **kwargs):
if created:
HighScore.objects.create(user=instance)
【讨论】:
这给了我刚刚添加到代码末尾的错误堆栈。 错误不清楚,能否请您在本地机器上运行代码,看看它抛出了什么错误? 不幸的是,我试图让它在我的本地机器上运行 HTTP 而不是 HTTPS,但即使将设置改回来,它也会出错,说它不能在本地机器上运行 HTTPS。我注释掉了SECURE_SSL_REDIRECT = True SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
和 class SSLMiddleware(object): <...> return HttpResponseRedirect(secure_url)`` and still getting this error for local machine:
code 400,消息 Bad request syntax ('\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03®¿........)您正在通过 HTTPS 访问开发服务器,但它仅支持 HTTP。
您的错误正在发生,因为您的表中没有列用户 ID。您应该运行 makemigration 和 migrate 命令
我都做了,但仍然遇到同样的错误。当我在 Heroku 上迁移时,它显示“没有要应用的迁移”。【参考方案2】:
错误告诉您发生了什么:您只是没有将新列添加到数据库中。
您需要迁移。在本地机器上运行 makemigrations
,提交结果,推送到 Heroku,然后在 Heroku 上运行 migrate
。
注意,你真的不应该直接在 Heroku 上进行开发。在本地运行开发服务器。
【讨论】:
不幸的是,我试图让它在我的本地机器上运行 HTTP 而不是 HTTPS,但即使将设置改回来,它也会出错,说它不能在本地机器上运行 HTTPS。我注释掉了 SECURE_SSL_REDIRECT = True SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') 和 class SSLMiddleware(object): <...> return HttpResponseRedirect(secure_url)`` 并且仍然在本地机器上收到此错误:代码 400,消息错误请求语法 ('\x16\x03\x01\x02\x00\x01\x00\x01ü\x03\x03®¿........) 您正在通过 HTTPS 访问开发服务器,但它仅支持 HTTP . 我都做了,但仍然遇到同样的错误。当我在 Heroku 上迁移时,它显示“没有要应用的迁移。” 那你还没有按我说的做。您是否将迁移文件添加到 git?然后你将新的提交推送到 Heroku 了吗? (正如该错误所说,您需要使用 http:// 而不是 https:// 来访问开发服务器。) 感谢您的帮助。登录页面在 http 上本地工作,但 /auth/register/ 和 /highscores/ 页面仍然无法工作,即使我输入 http:// 或没有 url 的基础。不确定它是否正在重定向,但它不会带我到 http。另外,我相信我完全按照您所说的在本地运行 migrate 和 makemigrations,推送到 git 和 heroku,并在 heroku 上进行了 makemigrations 和 migrate。谢谢。 编辑:使用邮递员在本地得到同样的错误说列 edjudicator game_highscore.user_id 在运行 makemigrations 和 migrate 后不存在(这次我先删除了迁移文件)。谢谢。以上是关于使用 Auth、用户模型、视图、序列化程序的 Django API OneToOne 关系的主要内容,如果未能解决你的问题,请参考以下文章
DRF 序列化程序错误:AttributeError:“FeedPostSerializer”对象没有属性“auth_user”