아래 그림과 같이 두 개의 사용자 정의 필드가 있습니다.사용자 지정 필드에 쿼리 세트를 만드는 방법은 무엇입니까?
사용자가 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()),
),
}
***
ERROR :
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
오류 UNION :
AttributeError: 'Permission' object has no attribute '_combinator_query'
여기에 2 가지 질문을하고 있습니다. 한 번에 하나의 질문 만 제출하십시오. 'model.objects.none()'을 사용하여 빈 쿼리 세트를 생성 한 다음,'QuerySet.union()'또는 합집합 연산자'|'를 사용하여 세트와 비슷한 아이템을 추가 할 수 있습니다. 그러나 이것은 다소 우회적이며 아마도 비효율적 인 접근 방법입니다. –
당신이 말한 것처럼'.union' 메서드를 사용하려고했지만 에러가 발생합니다. = (의견이 있으십니까? pls로 코드를 업데이트하십시오. –