Django表单为组中的用户显示额外的类别



我想在下面向拥有博主类别的用户额外展示这些类别。

目前的问题是…

  1. 对于博主用户来说,它只显示博主类别,而应该显示所有类别
  2. 对于普通用户来说,它显示的是博主类别,而本应只显示原始的3

forms.py

class PostCreateForm(ModelForm):

class Meta:
model = Post
fields = [
"title",
"category",
"associated_portfolios",
"body",
]
exclude = ('allow_comments',)

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
blogger = User.objects.filter(groups__name='blogger').exists()
print("Current User:", user)
print("Blogger User:",  blogger)
super(PostCreateForm, self).__init__(*args, **kwargs)
self.fields['category'].choices = (
("Watchlist", "Watchlist"),
("Lesson/Review", "Lesson/Review"),
("General", "General"),
)
if User.objects.filter(groups__name='blogger').exists():
self.fields['category'].choices = (
("Analysis", "Analysis"),
("Milestone", "Milestone"),
("Features", "Features"),
("Tutorials", "Tutorials"),
("Careers", "Careers"),
("Community", "Community"),
)

您可以尝试以下操作definit(self、*args、**kwargs(:user=kwargs.pop('user',无(

super(PostCreateForm, self).__init__(*args, **kwargs)
if user and user.groups.filter(name = 'blogger').exists():
# show all categories if user is in blogger group
self.fields['category'].choices = (
("Analysis", "Analysis"),
("Milestone", "Milestone"),
("Features", "Features"),
("Tutorials", "Tutorials"),
("Careers", "Careers"),
("Community", "Community"),
("Watchlist", "Watchlist"),
("Lesson/Review", "Lesson/Review"),
("General", "General"),
)
else:
# show basic categories if user is not in blogger group.
self.fields['category'].choices = (
("Watchlist", "Watchlist"),
("Lesson/Review", "Lesson/Review"),
("General", "General"),
)

检查用户是否在组中,并显示博主的类别

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
super(PostCreateForm, self).__init__(*args, **kwargs)
if user and user.groups.filter(name = 'blogger').exists():
# show all categories if user is in blogger group
self.fields['category'].choices = (
("Analysis", "Analysis"),
("Milestone", "Milestone"),
("Features", "Features"),
("Tutorials", "Tutorials"),
("Careers", "Careers"),
("Community", "Community"),
)
else:
# show basic categories if user is not in blogger group.
self.fields['category'].choices = (
("Watchlist", "Watchlist"),
("Lesson/Review", "Lesson/Review"),
("General", "General"),
)

如果你想检查用户是否在群博主中,并以此为基础向他显示不同的类别,你应该这样做:

class PostCreateForm(ModelForm):

class Meta:
model = Post
fields = [
"title",
"category",
"associated_portfolios",
"body",
]
exclude = ('allow_comments',)

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None)
# blogger = User.objects.filter(groups__name='blogger').exists()
# print("Current User:", user)
# print("Blogger User:",  blogger)
super(PostCreateForm, self).__init__(*args, **kwargs)

if User.objects.filter(groups__name='blogger', id=user.id).exists():
self.fields['category'].choices = (
("Analysis", "Analysis"),
("Milestone", "Milestone"),
("Features", "Features"),
("Tutorials", "Tutorials"),
("Careers", "Careers"),
("Community", "Community"),
)
else:
self.fields['category'].choices = (
("Watchlist", "Watchlist"),
("Lesson/Review", "Lesson/Review"),
("General", "General"),
)

您需要检查在kwargs中传递给表单的用户是否有博主组。甚至比更好

User.objects.filter(groups__name='blogger', id=user.id).exists()

正在使用

user.groups.filter(name='blogger').exists()

直接在用户实例上

最新更新