Django - Overriding the Model.create() method?

后端 未结 7 963
忘了有多久
忘了有多久 2020-12-04 08:36

The Django docs only list examples for overriding save() and delete(). However, I\'d like to define some extra processing for my models onl

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-04 09:42

    The preferred answer is correct but the test to tell whether the object is being created doesn't work if your model derives from UUIDModel. The pk field will already have a value.

    In this case, you can do this:

    already_created = MyModel.objects.filter(pk=self.pk).exists()

提交回复
热议问题