Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

377
Views
How to pass data to serializers in django

I want to pass user_id from view to serializer I have model Answer

class Answer(models.Model) :
    
    text = models.CharField(max_length=500)
    question_id = models.CharField(max_length=25)
    user_id = models.CharField(max_length=25, default=1)

This is my Serializer

class CreateAnswer(generics.CreateAPIView) :

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context["id"] = self.request.user.id
        return context

    serializer_class = AnswerQuestionSerializer
    queryset = Answer.objects.all()

What I need to write in my view to take user_id and create model with this user_id ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can override the perform_create method & pass the user_id field to save method of the serializer.

class CreateAnswerView(generics.CreateAPIView) :
    serializer_class = AnswerQuestionSerializer

    def perform_create(self, serializer):
        serializer.save(user_id=self.request.user.id)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!