Why does django ORM's `save` method not return the saved object?

前端 未结 3 788
离开以前
离开以前 2020-12-09 07:11

Any insight into the reasoning behind this design decision? It seems to me that having obj.save() return something, has only benefits (like method chai

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 08:15

    Since this is the first result that I get when searching for "django return saved object", to compliment Andrew's answer, if you still want to return the saved object, instead of using:

    ExampleModel(title=title).save()
    

    which returns None, you'd use:

    saved_instance = ExampleModel.objects.create(title=title)
    

    And this works because ExampleModel.objects is a Model Manager rather than an instance of the class, so it's not returning itself.

提交回复
热议问题