Django ModelAdmin admin_order_field with nulls_last - django

I am trying to sort column of ModelAdmin list field with admin_order_field with nulls_last such as:
class UserProfileAdmin(admin.ModelAdmin):
def get_sum_amount(self, obj):
return obj.sum_amount
get_sum_amount.admin_order_field = F('sum_amount').desc(nulls_last=True)
But I am getting following error:
Traceback (most recent call last):
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/contrib/admin/options.py", line 606, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/utils/decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1672, in changelist_view
cl = self.get_changelist_instance(request)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/contrib/admin/options.py", line 744, in get_changelist_instance
sortable_by,
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 81, in __init__
self.queryset = self.get_queryset(request)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/contrib/admin/views/main.py", line 436, in get_queryset
qs = qs.order_by(*ordering)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/db/models/query.py", line 1074, in order_by
obj.query.add_ordering(*field_names)
File "/home/petr/.local/share/virtualenvs/aklub_project-HrPEZ5ak/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1804, in add_ordering
if not hasattr(item, 'resolve_expression') and not ORDER_PATTERN.match(item):
TypeError: expected string or bytes-like object
I am using Django 2.2. Is this issue in Django or am I doing something wrong?

This is now possible in Django starting with version 3.1.

Related

"Cannot call delete() after .distinct()" error after django update

I am using djangocms_blog.
Today I updated django from 3.1 to 3.2.12 and suddenly when I try to delete post object, there is "Cannot call delete() after .distinct()" error
Traceback:
env\lib\site-packages\django\core\handlers\exception.py, line 47, in inner
response = get_response(request) …
env\lib\site-packages\django\core\handlers\base.py, line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) …
env\lib\site-packages\django\contrib\admin\options.py, line 616, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs) …
env\lib\site-packages\django\utils\decorators.py, line 130, in _wrapped_view
response = view_func(request, *args, **kwargs) …
env\lib\site-packages\django\views\decorators\cache.py, line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs) …
env\lib\site-packages\django\contrib\admin\sites.py, line 232, in inner
return view(request, *args, **kwargs) …
env\lib\site-packages\django\utils\decorators.py, line 43, in _wrapper
return bound_method(*args, **kwargs) …
env\lib\site-packages\django\utils\decorators.py, line 130, in _wrapped_view
response = view_func(request, *args, **kwargs) …
env\lib\site-packages\django\contrib\admin\options.py, line 1739, in changelist_view
response = self.response_action(request, queryset=cl.get_queryset(request)) …
env\lib\site-packages\django\contrib\admin\options.py, line 1408, in response_action
response = func(self, request, queryset) …
env\lib\site-packages\django\contrib\admin\actions.py, line 45, in delete_selected
modeladmin.delete_queryset(request, queryset) …
env\lib\site-packages\django\contrib\admin\options.py, line 1109, in delete_queryset
queryset.delete() …
env\lib\site-packages\django\db\models\query.py, line 728, in delete
raise TypeError('Cannot call delete() after .distinct().') …
I restored Django 3.1 version and everything works fine, but working on old version isn't best solution
Fixed via setting
Django = ">=3.2,<3.3"
in pyproject.toml, so it selected itself Django 3.2.13.

TypeError at /oauth/complete/mediawiki/ when upgrading to social-auth-app-django 3.3.0

I'm running:
Python 3.7.3
Django 3.1.14
social-auth-app-django 3.1.0
When I try to upgrade social-auth-app-django to 3.3.0 or 3.4.0, I get:
Traceback (most recent call last):
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_django/utils.py", line 49, in wrapper
return func(request, backend, *args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_django/views.py", line 33, in complete
*args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/actions.py", line 45, in do_complete
user = backend.complete(user=user, *args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/backends/base.py", line 40, in complete
return self.auth_complete(*args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/utils.py", line 247, in wrapper
return func(*args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/backends/oauth.py", line 181, in auth_complete
return self.do_auth(access_token, *args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/utils.py", line 247, in wrapper
return func(*args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/backends/oauth.py", line 192, in do_auth
return self.strategy.authenticate(*args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_django/strategy.py", line 107, in authenticate
return authenticate(*args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/contrib/auth/__init__.py", line 73, in authenticate
user = backend.authenticate(request, **credentials)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/backends/base.py", line 80, in authenticate
return self.pipeline(pipeline, *args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/backends/base.py", line 83, in pipeline
out = self.run_pipeline(pipeline, pipeline_index, *args, **kwargs)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/backends/base.py", line 113, in run_pipeline
result = func(*args, **out) or {}
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/social_core/pipeline/user.py", line 119, in user_details
setattr(user, name, value)
File "/data/project/spi-tools-dev/www/python/venv/lib/python3.7/site-packages/django/db/models/fields/related_descriptors.py", line 547, in __set__
% self._get_set_deprecation_msg_params(),
Exception Type: TypeError at /oauth/complete/mediawiki/
Exception Value: Direct assignment to the forward side of a many-to-many set is prohibited. Use groups.set() instead.
when I try to authenticate. Any idea what's going on?
The TL;DR is to add
SOCIAL_AUTH_PROTECTED_USER_FIELDS = ['groups']
to settings.py. See here for the longer version.

AttributeError: 'NoneType' object has no attribute 'get' at openEdx exchange_token api

I was working on the azure ad login for openEdx mobile app and as mentioned in the title, I got the NoneType response at token_exchange endpoint from azure ad.
Azure login have no problem authentication the user from the website login but login from the mobile got 'Service unavailable response' due to the following error log on the server.
I suspect the configuration issue but as far as I checked, i only need to set 'SOCIAL_AUTH_OAUTH_SECRETS' from lms.auth.json and 'THIRD_PARTY_AUTH_BACKENDS' and 'ENABLE_THIRD_PARTY_AUTH' from lms.env.json.
Please let me know if I miss something out. Thanks in advance
Apr 1 12:38:45 edxapp [service_variant=lms][django.request][env:sandbox] ERROR [edxapp 24509] [exception.py:135] - Internal Server Error: /oauth2/exchange_access_token/azuread-oauth2/
Traceback (most recent call last):
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py", line 185, in inner
return func(*args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/oauth_dispatch/views.py", line 57, in dispatch
return view(request, *args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/social_django/utils.py", line 49, in wrapper
return func(request, backend, *args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/auth_exchange/views.py", line 44, in dispatch
return super(AccessTokenExchangeBase, self).dispatch(*args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py", line 489, in dispatch
response = self.handle_exception(exc)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py", line 449, in handle_exception
self.raise_uncaught_exception(exc)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/rest_framework/views.py", line 486, in dispatch
response = handler(request, *args, **kwargs)
File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/auth_exchange/views.py", line 57, in post
if not form.is_valid():
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/forms/forms.py", line 183, in is_valid
return self.is_bound and not self.errors
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/forms/forms.py", line 175, in errors
self.full_clean()
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/forms/forms.py", line 385, in full_clean
self._clean_form()
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/provider/forms.py", line 63, in _clean_form
super(OAuthForm, self)._clean_form()
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/django/forms/forms.py", line 412, in _clean_form
cleaned_data = self.clean()
File "/edx/app/edxapp/edx-platform/openedx/core/djangoapps/auth_exchange/forms.py", line 95, in clean
user = backend.do_auth(access_token, allow_inactive_user=True)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/social_core/utils.py", line 252, in wrapper
return func(*args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/social_core/backends/oauth.py", line 410, in do_auth
data = self.user_data(access_token, *args, **kwargs)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/social_core/backends/azuread.py", line 80, in user_data
id_token = response.get('id_token')
AttributeError: 'NoneType' object has no attribute 'get'

django.urls.exceptions.NoReverseMatch: Reverse for 'account_inactive' not found. 'account_inactive' is not a valid view function or pattern name

I am using django-rest-auth registration api, I overridden that by using my own SERIALIZER and model. So I am getting error.
I have tried by setting active at the time user created. But that is not working.
Let me know how to resolve this type of problem.
[12/Jan/2019 17:24:06] "POST /rest-auth/registration/ HTTP/1.1" 400 68
Internal Server Error: /rest-auth/registration/
Traceback (most recent call last):
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/utils/decorators.py", line 45, in _wrapper
return bound_method(*args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/views/decorators/debug.py", line 76, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_auth/registration/views.py", line 46, in dispatch
return super(RegisterView, self).dispatch(*args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_framework/generics.py", line 192, in post
return self.create(request, *args, **kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_auth/registration/views.py", line 65, in create
user = self.perform_create(serializer)
File "/Users/test/project/vblah/lib/python3.6/site-packages/rest_auth/registration/views.py", line 81, in perform_create
None)
File "/Users/test/project/vblah/lib/python3.6/site-packages/allauth/account/utils.py", line 183, in complete_signup
signal_kwargs=signal_kwargs)
File "/Users/test/project/vblah/lib/python3.6/site-packages/allauth/account/utils.py", line 133, in perform_login
return adapter.respond_user_inactive(request, user)
File "/Users/test/project/vblah/lib/python3.6/site-packages/allauth/account/adapter.py", line 454, in respond_user_inactive
reverse('account_inactive'))
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/urls/base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/test/project/vblah/lib/python3.6/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'account_inactive' not found. 'account_inactive' is not a valid view function or pattern name.```
this exception happend because in your User model
is_active = False .
so set True and migrate
in models.py set is_active = True
and migrate the tables

asyncio in django view

2018-05-30 09:53:21 [exception ERROR] Internal Server Error: /of/template/team
Traceback (most recent call last):
File "/Users/wangchao/.virtualenvs/ENV_PY3.6_zbxtree/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/wangchao/.virtualenvs/ENV_PY3.6_zbxtree/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/wangchao/.virtualenvs/ENV_PY3.6_zbxtree/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/wangchao/.virtualenvs/ENV_PY3.6_zbxtree/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/Users/wangchao/.virtualenvs/ENV_PY3.6_zbxtree/lib/python3.6/site-packages/django/contrib/auth/mixins.py", line 56, in dispatch
return super(LoginRequiredMixin, self).dispatch(request, *args, **kwargs)
File "/Users/wangchao/.virtualenvs/ENV_PY3.6_zbxtree/lib/python3.6/site-packages/django/views/generic/base.py", line 88, in dispatch
return handler(request, *args, **kwargs)
File "/Users/wangchao/.virtualenvs/ENV_PY3.6_zbxtree/lib/python3.6/site-packages/django/views/generic/base.py", line 155, in get
context = self.get_context_data(**kwargs)
File "/Users/wangchao/Documents/SenseTime/sencetime_git/wangchao3/zabbixbiztree/biztree/of/views/template_team.py", line 33, in get_context_data
self.test()
File "/Users/wangchao/Documents/SenseTime/sencetime_git/wangchao3/zabbixbiztree/biztree/of/views/template_team.py", line 29, in test
result = loop.run_until_complete(asyncio.gather(*tasks))
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py", line 596, in gather
fut = ensure_future(arg, loop=loop)
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/tasks.py", line 518, in ensure_future
loop = events.get_event_loop()
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py", line 676, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py", line 584, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'Thread-2'.
2018-05-30 09:53:21 [basehttp ERROR] "GET /of/template/team HTTP/1.1" 500 118472
class TemplateTeamView(LoginRequiredMixin,TemplateView):
template_name = 'of/template_team.html'
def get_context_data(self, **kwargs):
loop = asyncio.get_event_loop()
tasks = [of._get_detail_template(i) for i in [1,2,3,4]]
result = loop.run_until_complete(asyncio.gather(*tasks))
context = super(TemplateTeamView, self).get_context_data(**kwargs)
context.update({"result": result})
return context
how to design calling multi APIs to gather result before render template?