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

192
Views
¿Cómo serializar un modelo con muchas o muchas relaciones?

Tengo esta función en mi modelo,

 def serialize(self): return { 'id': self.id, 'author': self.author.username, 'text': self.text, 'timestamp': self.timestamp.strftime("%b %d %Y, %I:%M %p"), 'likes': self.likes.all(), 'likes_number': len(self.likes.all()), }

pero los gustos son en realidad una relación de muchos a muchos con el usuario. ¿Cómo puedo serializarlo para obtener algo como esto?

 def serialize(self): return { 'id': self.id, 'author': self.author.username, 'text': self.text, 'timestamp': self.timestamp.strftime("%b %d %Y, %I:%M %p"), 'likes': [ user1, user2, etc. ], }

Para que también pueda deshacerme de la propiedad 'número de me gusta'.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puede usar la comprensión de listas para serializar sus datos, pero usar el serializador de django rest framework es más apropiado.

 return { 'id': self.id, 'author': self.author.username, 'text': self.text, 'timestamp': self.timestamp.strftime("%b %d %Y, %I:%M %p"), 'likes': [{'id':like.id,...} for like self.likes.all()], 'likes_number': len(self.likes.all()), }
about 4 years ago · Juan Pablo Isaza 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!