使用序列化程序使用 ForeignKey 更新 django 模型数据库
Update django model database with ForeignKey by using serializer
我创建了一个 django model
,其中包含用户的外键,如下所示:
from authentication.models import User
from django.db import models
class Event(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
dr_notice_period = models.IntegerField(blank=True, null=True)
dr_duration = models.IntegerField(blank=True, null=True)
dr_request = models.FloatField(blank=True, null=True)
我的serializers.py
文件如下:
class EventSerializer(serializers.ModelSerializer):
user = UserSerializer(many=True, read_only=True)
class Meta:
model = Event
fields = ['user', 'dr_notice_period', 'dr_duration', 'dr_request']
我需要做的是转到 url 并通过 POST 请求将数据上传到数据库,但没有具体指定用户。
我的views.py
如下:
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework import status
from vpp_optimization.serializers import EventSerializer
@api_view(['POST'])
def event(request):
serializer = EventSerializer(data=request.data)
if serializer.is_valid():
instance = serializer.save(commit=False)
instance.user = request.user
instance.save()
return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
else:
return Response({"status": "error", "data": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
在我研究的过程中,我认为通过在 save
中使用 commit=False
可以解决问题,但我收到以下错误:
'commit' is not a valid keyword argument to the 'save()' method. If you need to access data before committing to the database then inspect 'serializer.validated_data' instead. You can also pass additional keyword arguments to 'save()' if you need to set extra attributes on the saved model instance. For example: 'serializer.save(owner=request.user)'.'
有没有更好的方法来完成我打算做的事情?
您将用户作为参数传递,因此:
if serializer.is_valid():
instance = serializer.save(<strong>user=request.user</strong>)
return Response({'status': 'success', 'data': serializer.data}, status=status.HTTP_200_OK)
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
我创建了一个 django model
,其中包含用户的外键,如下所示:
from authentication.models import User
from django.db import models
class Event(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
dr_notice_period = models.IntegerField(blank=True, null=True)
dr_duration = models.IntegerField(blank=True, null=True)
dr_request = models.FloatField(blank=True, null=True)
我的serializers.py
文件如下:
class EventSerializer(serializers.ModelSerializer):
user = UserSerializer(many=True, read_only=True)
class Meta:
model = Event
fields = ['user', 'dr_notice_period', 'dr_duration', 'dr_request']
我需要做的是转到 url 并通过 POST 请求将数据上传到数据库,但没有具体指定用户。
我的views.py
如下:
from rest_framework.response import Response
from rest_framework.decorators import api_view
from rest_framework import status
from vpp_optimization.serializers import EventSerializer
@api_view(['POST'])
def event(request):
serializer = EventSerializer(data=request.data)
if serializer.is_valid():
instance = serializer.save(commit=False)
instance.user = request.user
instance.save()
return Response({"status": "success", "data": serializer.data}, status=status.HTTP_200_OK)
else:
return Response({"status": "error", "data": serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
在我研究的过程中,我认为通过在 save
中使用 commit=False
可以解决问题,但我收到以下错误:
'commit' is not a valid keyword argument to the 'save()' method. If you need to access data before committing to the database then inspect 'serializer.validated_data' instead. You can also pass additional keyword arguments to 'save()' if you need to set extra attributes on the saved model instance. For example: 'serializer.save(owner=request.user)'.'
有没有更好的方法来完成我打算做的事情?
您将用户作为参数传递,因此:
if serializer.is_valid():
instance = serializer.save(<strong>user=request.user</strong>)
return Response({'status': 'success', 'data': serializer.data}, status=status.HTTP_200_OK)
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.