格式化权限查询集,使其仅显示名称列
Formatting a permissions QuerySet so that it just displays the name column
我正在尝试通过查询列出特定于某个模型的权限。目标是仅显示 QuerySet 的 "name" 部分。我只能通过标签获得以下输出:{{ permissions }}
<QuerySet [<Permission: cms | Account meta | Can add Account meta>, <Permission: cms | Account meta | Can change Account meta>, <Permission: cms | Account meta | Can delete Account meta>, <Permission: cms | Account meta | Allows the user to log in as others>, <Permission: cms | Account meta | Allows a user to deactivate/delete other users>, <Permission: cms | Account meta | Allows a user to edit site and role details>, <Permission: cms | Account meta | Can export user details>, <Permission: cms | Account meta | Can view Account meta>]>
models.py
class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
site = models.CharField(max_length=50, choices=(('all', 'all'), ('danielk', 'danielk'), ('flis', 'flis'), ('vmusic', 'vmusic')), blank=True)
site_role = models.CharField(max_length=50, choices=(('Administrator', 'Administrator'), ('Moderator', 'Moderator'), ('Editor', 'Editor')))
phone_number = models.CharField(max_length=8)
birth_date = models.DateField()
street_adress = models.CharField(max_length=255)
note = models.TextField(blank=True);
zip_code = models.CharField(max_length=4)
city = models.CharField(max_length=255)
def formatedPhone(self, country=None):
return phonenumbers.parse(Account.phone_number, "NO")
"""
def __str__(self):
return "%s %s" % (self.User.first_name, self.user.last_name)
"""
def get_absolute_url(self):
return reverse('account-detail', kwargs={'pk': self.pk})
class Meta:
verbose_name = 'Account meta'
verbose_name_plural = 'Accounts meta'
permissions = (
("has_user_hijack_permission", "Allows the user to log in as others"),
("has_user_management", "Allows a user to deactivate/delete other users"),
("has_user_site_edit", "Allows a user to edit site and role details"),
("has_user_takeout", "Can export user details"),
)
views.py
class cms_users_user_permissions(generic.DetailView):
model = Account
template_name = 'cms/users/cms-users-user-permissions.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["permissions"] = Permission.objects.filter(content_type=ContentType.objects.get_for_model(Account))
#context['permissions'] = Permission.objects.filter(content_type_id=7)
return context
不是写 <s>{{ permissions }}</s>
,而是遍历查询集,每次打印权限的名称,喜欢:
{% for <b>permission in permissions</b> %}
{{ permission.name }}
{% endfor %}
我正在尝试通过查询列出特定于某个模型的权限。目标是仅显示 QuerySet 的 "name" 部分。我只能通过标签获得以下输出:{{ permissions }}
<QuerySet [<Permission: cms | Account meta | Can add Account meta>, <Permission: cms | Account meta | Can change Account meta>, <Permission: cms | Account meta | Can delete Account meta>, <Permission: cms | Account meta | Allows the user to log in as others>, <Permission: cms | Account meta | Allows a user to deactivate/delete other users>, <Permission: cms | Account meta | Allows a user to edit site and role details>, <Permission: cms | Account meta | Can export user details>, <Permission: cms | Account meta | Can view Account meta>]>
models.py
class Account(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
site = models.CharField(max_length=50, choices=(('all', 'all'), ('danielk', 'danielk'), ('flis', 'flis'), ('vmusic', 'vmusic')), blank=True)
site_role = models.CharField(max_length=50, choices=(('Administrator', 'Administrator'), ('Moderator', 'Moderator'), ('Editor', 'Editor')))
phone_number = models.CharField(max_length=8)
birth_date = models.DateField()
street_adress = models.CharField(max_length=255)
note = models.TextField(blank=True);
zip_code = models.CharField(max_length=4)
city = models.CharField(max_length=255)
def formatedPhone(self, country=None):
return phonenumbers.parse(Account.phone_number, "NO")
"""
def __str__(self):
return "%s %s" % (self.User.first_name, self.user.last_name)
"""
def get_absolute_url(self):
return reverse('account-detail', kwargs={'pk': self.pk})
class Meta:
verbose_name = 'Account meta'
verbose_name_plural = 'Accounts meta'
permissions = (
("has_user_hijack_permission", "Allows the user to log in as others"),
("has_user_management", "Allows a user to deactivate/delete other users"),
("has_user_site_edit", "Allows a user to edit site and role details"),
("has_user_takeout", "Can export user details"),
)
views.py
class cms_users_user_permissions(generic.DetailView):
model = Account
template_name = 'cms/users/cms-users-user-permissions.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["permissions"] = Permission.objects.filter(content_type=ContentType.objects.get_for_model(Account))
#context['permissions'] = Permission.objects.filter(content_type_id=7)
return context
不是写 <s>{{ permissions }}</s>
,而是遍历查询集,每次打印权限的名称,喜欢:
{% for <b>permission in permissions</b> %}
{{ permission.name }}
{% endfor %}