I have a issuetracker project where a manager can assign a user to a project. The manager selects a project and a user from dropdowns, clicks submit, and an api call is called in the frontend to send that post data to my django APIView assignuser. Assignuser then takes both those values and filters for the correct project and the user, and saves that user to the project user. After checking the projects, I am unable to save a new User to a project as User comes up as null. Is there a specified way in saving foreign keys in the DRF?
assignuser
class assignuser(APIView):
serializer_class = ProjectSerializer
def post(self, request, format=None):
serializer = self.serializer_class(data=request.data)
if serializer.is_valid():
user_name = serializer.get('user')
project_name = serializer.get('project_name')
user = Users.objects.get(user=user_name)
project = Project.objects.get(name=project_name)
project.user = user
project.save()
return Response(ProjectSerializer(project).data, status=status.HTTP_201_CREATED)
else:
return HttpResponse("Incorrect Credentials")
Project Serializer
class ProjectSerializer(serializers.ModelSerializer):
user = SignUpSerializer()
class Meta:
model = Project
fields = ('name', 'description', 'user')
SignUp Serializer
class SignUpSerializer(serializers.ModelSerializer):
class Meta:
model = Users
fields = ('username', 'password', 'email', 'role')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…