且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改模型字段的选择属性

更新时间:2023-02-12 08:37:50

我刚发现:

self.fields['project'].choices = [('', '----------')] + [(p.id, p.name) for p in Project.objects.all()]

在RecordForm的 init 中也可以做到这一点.但是@Peter DeGlopper在评论中的答案更好.

In the init of the RecordForm also does the trick. But @Peter DeGlopper answer in the comments is better.