在ModelForm中限制条件选择

时间:2018-04-22 16:25:40

标签: django modelform choice

不明白如何限制用户组的字段选择(django admin中的标准用户组)

views.py

ACTUAL = 'Actual'
IRRELEVANT = 'Irrelevant'
REWORK = 'Rework'
CHECKING = 'Checking'

DOCUMENT_STATUS_OF_GROUP = [ACTUAL, IRRELEVANT, REWORK, CHECKING]

class UpdateDocumentForm(forms.ModelForm):
    class Meta:
       model = Document
       fields = ('name', 'status')

        def __init__(self, *args, **kwargs):
           group = kwargs.pop('groups')
           super(UpdateDocumentForm, self).__init__(*args, **kwargs)
           if group == 'Controler':
                self.fields['status'].choices = DOCUMENT_STATUS_OF_GROUP

models.py

class Document(models.Model):
    ACTUAL = 'Actual'
    INWORK = 'InWork'
    IRRELEVANT = 'Irrelevant'
    REWORK = 'Rework'
    CHECKING = 'Checking'

    DOCUMENT_STATUS = [ACTUAL, IRRELEVANT, REWORK, CHECKING, INWORK]

    name = models.CharField(max_length=50)
    status = models.TextField(default=IRRELEVANT, choices=[(x, x) for x in DOCUMENT_STATUS])

此代码不限制选择 - 在表单中显示所有字段选择

0 个答案:

没有答案