Django many-to-many model DRF

前端 未结 5 1511
执念已碎
执念已碎 2021-01-26 04:37

I have the following model structure:

class Project(models.Model):
  author = models.ManyToManyField(Account)
  name = models.CharField(max_length=40, default=\'         


        
5条回答
  •  广开言路
    2021-01-26 05:23

    Ok, my previous answer, though could be an issue, isn't the root cause of the actual crash.

    When calling the serializer, you set:

    instance = serializer.save(author=self.request.user)
    

    However, author is a ManyToManyField which means you should call the serializer as:

    instance = serializer.save(author=[self.request.user])
    

    NB: you still require the many=True on the serializer's author field.

提交回复
热议问题