Adding field that isn't in model to serializer in Django REST framework

前端 未结 4 2230
孤独总比滥情好
孤独总比滥情好 2020-12-28 21:24

I have a model Comment that when created may or may not create a new user. For this reason, my API requires a password field when creating a new comment. Here is my Comment

4条回答
  •  既然无缘
    2020-12-28 22:17

    What you can do is to overwrite the pre_save or create function and take out the commenter_pw from the data fields that are sent (not sure, but you can probably take it out form request.POST or after you have serialized it), so the framework should not rise the error.

    Plus, if you have additional logic you can implement it there before saving it (such as the one for checking if the user have to be created or what).

提交回复
热议问题