Selection字段的ondelete属性
-
当我们在自己模块里继承了某个模型并且通过
selection_add对这个模型的Selection类型字段添加了新的选项。系统中的记录也选择了我们定义的选项。然后,由于某些情况,我们需要卸载这个模块,那么这些选择了我们新的选项的记录这时候应该怎么处理,尤其是如果这个选项类型的字段还是“Required"的字段,那就更会引起错误了。如
fields.Many2one字段一样,为了处理fields.Selection出现上诉的问题,我们也要为这个字段添加ondelete属性。ondelete属性对应一个python字典。当该字段的
required=True时,ondelete属性是必须设置的,并且不能其值设置为null比如:
my_selection = fields.Selection(selection_add=[ ('pikachu', "Pikachu"), ('eevee', "Eevee"), ], ondelete={'pikachu': 'default', 'eevee': 'set bar'})从上面的例子可以看到,这个
ondelete字典的key是新添的选项,其值就是当新添的选项删除时(模块卸载时)需要处理的动作,这些动作包括:- 'null' --默认值,当对应选项删除时会将该字段的值设为False。如上所诉,当该字段的
required=True时ondelete不能使用这个动作。 - 'cascade' -- 所有选择当前选项的记录,当选项被删除时,记录一并被删除。
- 'default' -- 选择该选项的所有记录,当选项被删除时,该字段恢复为默认值。
- 'set ' -- 选择该选项的所有记录,当选项被删除时,该字段的值设为这里的设定值,这里的设定值必须有效存在。
<callable>-- 可以定义一个方法来返回结果,这个函数只有一个唯一参数对应所有包含该选项的记录。
- 'null' --默认值,当对应选项删除时会将该字段的值设为False。如上所诉,当该字段的
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login