フォームログインユーザーのユーザーグループに応じて、ログインユーザーが特定のグループに属していない場合、一部のstory_status値を削除する必要があります。選択肢フィールド値footage ready
からstory_status
。私のコードは値を除外していません
models.py
class Article(models.Model):
STORY_STATUS = {
('story not done', 'story not done'),
('story finish', 'story finish'),
('Copy Editor Done', 'Copy Editor Done'),
('footage ready', 'footage ready')
}
title = models.CharField(max_length=255, help_text="Short title")
story_status = models.CharField(choices=STORY_STATUS)
output.html
class ArticleForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(ArticleForm, self).__init__(*args, **kwargs)
if not self.user.groups.filter(name__iexact='producer').exists():
self.queryset = Article.objects.exclude(story_status='footage ready')
class Meta:
model = Article
fields = [
'title',
'story_status'
]
次のようなinitメソッドを使用して選択肢を上書きしてみてください
STORY_STATUS = [
('story not done', 'story not done'),
('story finish', 'story finish'),
('Copy Editor Done', 'Copy Editor Done'),
('footage ready', 'footage ready')
]
story_status_config = {
'producer': ['footage ready'],
'other_group': ['story finish']
}
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('user', None)
super(ArticleForm, self).__init__(*args, **kwargs)
for group,exclude_vals in story_status_config:
if not self.user.groups.filter(name__iexact=group).exists():
self.fields['story_status'].choices = [x for x in STORY_STATUS if x[0] not in exclude_vals]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加