So when a user submits a form via frontend (Vue.js), I want to be able to set the created_by attribute in the backend. What is the best way of achieving this?
Views
class ProjectView(generics.RetrieveAPIView):
queryset = Project.objects.order_by('-created_at')
def get(self, request):
queryset = self.get_queryset()
serializer = ProjectsSerializer(queryset, many=True)
return Response(serializer.data)
def post(self, request):
if request.method == 'POST':
serializer = ProjectsSerializer
if serializer.is_valid():
serializer.save()
return JsonResponse(serializer.data, status=201)
return JsonResponse(serializer.errors, status=400)
Serializer
class ProjectsSerializer(serializers.ModelSerializer):
interest_category = serializers.StringRelatedField()
class Meta:
model = Project
fields = (
'project_title',
'project_description',
'interest_category',
'created_by',
'created_at',
'updated_at',
)
The data I am passing from Frontend
project_title: this.project_title,
project_description: this.project_description,
interest_category: this.interest_category,
I do have a token saved in localStorage, but I do not know how to get the ID of the currently logged in user id and pass to backend or set in backend. Any and all help is greatly appreciated!
What is the best way of setting created_by the submitting/requesting user in the backend?