我有2个自定义字段,如下图所示。
当用户发出GET
请求时,在右侧列表中我想显示用户的权限。在左侧列表中,我想显示尚未在右侧列表中的可用权限。如何为我的自定义字段生成正确的查询集?我很困惑。
多数民众赞成我认为我需要创建available_permissions
字段的查询集。伪代码:
for available_permission in Permission.objects.all()
if available_permission not in (QuerySet <user_permissions>)
Create new QuerySet with available_permission
1)如何了解用户的访问权限?我想在这里我需要将用户ID从视图发送到表单。
2)如何创建空查询集并将对象添加到该查询集?
forms.py:
class UserEditForm(UserChangeForm):
available_permissions = forms.ModelMultipleChoiceField(
widget=forms.SelectMultiple,
queryset=None,
required=False,
)
current_permissions = forms.ModelMultipleChoiceField(
widget=forms.SelectMultiple,
queryset=None,
required=False,
)
def __init__(self, *args, **kwargs):
self.current_permissions = kwargs.pop('current_permissions')
self.available_permissions = kwargs.pop('available_permissions')
self.fields['current_permissions'].queryset = self.current_permissions
self.fields['available_permissions'].queryset = self.available_permissions
views.py:
class UserEditView(UpdateView):
template_name = 'users/edit_user.html'
form_class = UserEditForm
model = User
def get(self, request, *args, **kwargs):
user = User.objects.get(pk=self.kwargs['pk'])
context = {
'user': user,
'user_edit_form': UserEditForm(
instance=user,
current_permissions=user.get_all_permissions(),
available_permissions=Permission.objects.difference(user.user_permissions.all()),
),
}
***
错误:
Traceback (most recent call last):
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Applications/Projects/web/dashboard.kase.kz/users/views.py", line 75, in get
data['html_form'] = render_to_string('users/edit_user.html', context, request=request)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/template/loader.py", line 68, in render_to_string
return template.render(context, request)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/template/defaulttags.py", line 173, in render
len_values = len(values)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/forms/boundfield.py", line 71, in __len__
return len(self.subwidgets)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/forms/boundfield.py", line 57, in subwidgets
for widget in self.field.widget.subwidgets(self.html_name, self.value(), attrs=attrs)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/forms/boundfield.py", line 56, in <genexpr>
BoundWidget(self.field.widget, widget, self.form.renderer)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/forms/widgets.py", line 561, in subwidgets
for option in self.options(name, value, attrs):
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/forms/widgets.py", line 566, in options
for group in self.optgroups(name, value, attrs):
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/forms/widgets.py", line 575, in optgroups
for index, (option_value, option_label) in enumerate(chain(self.choices)):
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/forms/models.py", line 1139, in __iter__
for obj in queryset:
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/db/models/query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 871, in execute_sql
sql, params = self.as_sql()
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/db/backends/oracle/compiler.py", line 21, in as_sql
with_col_aliases=with_col_aliases,
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 430, in as_sql
result, params = self.get_combinator_sql(combinator, self.query.combinator_all)
File "/Users/nurzhan_nogerbek/Virtualenvs/py2714/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 389, in get_combinator_sql
raise DatabaseError('ORDER BY not allowed in subqueries of compound statements.')
DatabaseError: ORDER BY not allowed in subqueries of compound statements.
UNION : 的 forms.py:
final_available_permissions = Permission.objects.none()
for available_permission in Permission.objects.all():
if available_permission not in self.current_permissions:
final_available_permissions = final_available_permissions.union(available_permission)
self.fields['available_permissions'].queryset = final_available_permissions
ERROR UNION:
AttributeError: 'Permission' object has no attribute '_combinator_query'
答案 0 :(得分:0)
您可以user.user_permissions.all()
获取用户的权限,Permission.objects.difference(user.user_permissions.all())
获得相反的权限。但是,如果你也考虑了群组权限,你必须做一些更复杂的事情。
另一种解决方案,因为您的设置中存在QuerySet.difference()
问题。
current_permissions = Permission.objects.filter(user=user)
available_permissions = Permission.objects.exclude(user=user)