(django) error while overriding save() - name 'self' is not defined - python

I'm trying to override the method save() of my model:
class Article(models.Model):
number = models.PositiveIntegerField(editable=False)
def save(self, *args, **kwargs):
if not self.pk: #means: if objects wasn't saved before
self.number = Article.objects.count() +1 #number is 1-based
super(Article, self).save(*args, **kwargs)
Now I tried to add a new article from the admin page. The error I get:
self.number = Article.objects.count() +1
NameError: name 'self' is not defined
What am I doing wrong?
Some optional information:
number is some kind of ID, to order the objects in my way but these IDs shouldn't be changed manually
number is not allowed to be empty or blank, so I have to set a value before saving the first time (that's what I'm trying with that code snippet)
When an object is saved, the default number should be equal to the number of all objects (after saving), in 1-based counting
to change the order of the objects admin actions will be used
The whole traceback
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 544, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/views/decorators/cache.py", line 57, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/sites.py", line 211, in inner
return view(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 1509, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 67, in _wrapper
return bound_func(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 149, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/utils/decorators.py", line 63, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/lib/python3.5/contextlib.py", line 30, in inner
return func(*args, **kwds)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 1449, in changeform_view
self.save_model(request, new_object, form, not add)
File "/usr/local/lib/python3.5/dist-packages/django/contrib/admin/options.py", line 1007, in save_model
obj.save()
File "/media/[directory to my projects]/projects_actual/myproject/myapp/models.py", line 69, in save
seĺf.number = Article.objects.count() + 1
NameError: name 'seĺf' is not defined
[16/Apr/2017 11:07:00] "POST /admin/myapp/article/add/ HTTP/1.1" 500 121043
Do you miss some information?

Related

How to log in in existing password account with google auth django?

I have problem with google auth. Steps to reproduce problem:
Register password account
Try to log in with google auth
Get 500 answer
Traceback:
Internal Server Error: /api/v1/auth/google/
Traceback (most recent call last):
File "/home/andrew/.local/lib/python3.10/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/andrew/.local/lib/python3.10/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/views/decorators/debug.py", line 89, in sensitive_post_parameters_wrapper
return view(request, *args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/dj_rest_auth/views.py", line 54, in dispatch
return super().dispatch(*args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
raise exc
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/andrew/Documents/pets_backend/project/api_v1/views/social.py", line 77, in post
response = super().post(request, *args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/dj_rest_auth/views.py", line 125, in post
self.serializer.is_valid(raise_exception=True)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/serializers.py", line 220, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/home/andrew/.local/lib/python3.10/site-packages/rest_framework/serializers.py", line 422, in run_validation
value = self.validate(value)
File "/home/andrew/.local/lib/python3.10/site-packages/dj_rest_auth/registration/serializers.py", line 151, in validate
complete_social_login(request, login)
File "/home/andrew/.local/lib/python3.10/site-packages/allauth/socialaccount/helpers.py", line 151, in complete_social_login
return _complete_social_login(request, sociallogin)
File "/home/andrew/.local/lib/python3.10/site-packages/allauth/socialaccount/helpers.py", line 172, in _complete_social_login
ret = _process_signup(request, sociallogin)
File "/home/andrew/.local/lib/python3.10/site-packages/allauth/socialaccount/helpers.py", line 22, in _process_signup
url = reverse("socialaccount_signup")
File "/home/andrew/.local/lib/python3.10/site-packages/django/urls/base.py", line 86, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "/home/andrew/.local/lib/python3.10/site-packages/django/urls/resolvers.py", line 694, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'socialaccount_signup' not found. 'socialaccount_signup' is not a valid view function or pa
ttern name.
"POST /api/v1/auth/google/ HTTP/1.1" 500 184486
I added to configs
SOCIALACCOUNT_EMAIL_VERIFICATION = "none"
SOCIALACCOUNT_EMAIL_REQUIRED = False
SOCIALACCOUNT_QUERY_EMAIL = False
but not worked.

Django forms Exception Value: __init__() got multiple values for argument 'data'

Positional argument and keyword argument collision for data=self.request.GET seems to cause this in the constructor of BaseForm as understood from the other posts. I recently upgraded from django 1.9 to django 3.2. Appreciate help in showing how I should pass the data argument to forms super constructor.
Traceback (most recent call last):
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/core/handlers/base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/views/generic/base.py", line 70, in view
return self.dispatch(request, *args, **kwargs)
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/dreamer/Projects/his/apps/internationals/views.py", line 46, in dispatch
return super(InternationalList, self).dispatch(request, *args, **kwargs)
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/views/generic/base.py", line 98, in dispatch
return handler(request, *args, **kwargs)
File "/home/dreamer/Envs/his/lib/python3.8/site-packages/django/views/generic/list.py", line 142, in get
self.object_list = self.get_queryset()
File "/home/dreamer/Projects/his/apps/internationals/views.py", line 59, in get_queryset
self.filter_form = InternationalFilterForm(self.request, data=self.request.GET)
File "/home/dreamer/Projects/his/apps/internationals/forms.py", line 141, in __init__
super(InternationalFilterForm, self).__init__(*args, **kwargs)
Exception Type: TypeError at /internationals/list/
Exception Value: __init__() got multiple values for argument 'data'
class InternationalList(views.OrderableListMixin, ListFilterMixin, generic.ListView):
...
def get_queryset(self):
if not self.request.GET.items():
self.filter_form = InternationalFilterForm(self.request)
else:
self.filter_form = InternationalFilterForm(self.request, data=self.request.GET)
class InternationalFilterForm(forms.Form):
...
def __init__(self, *args, **kwargs):
super(InternationalFilterForm, self).__init__(*args, **kwargs)
I believe data argument is been duplicate in BaseForm __init__ method.
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
initial=None, error_class=ErrorList, label_suffix=None,
empty_permitted=False, field_order=None, use_required_attribute=None, renderer=None)

Django error: too many values to unpack (expected 2) after adding model record

The idea
To make the code a bit more understandable, I will first explain what my code (from which the problem probably comes) is supposed to do in the first place: I save reports in my model. I give these reports their own ID or numbering, because this is absolutely necessary.This ID shall be structured as follows:
<year><ascending number with leading zeros>
Example: 2021001, 2021002, ..., 2022001
The code
I have developed the following code for this purpose. Since the value is to be calculated automatically, I use the #property decorator. To be able to use the ID later more easily as a field and simply for my REST Api, I use the computed_property package.
Extract from models.py:
einsatznummer = ComputedTextField(blank=True, compute_from="einsatznummer_calc")
#property
def einsatznummer_calc(self):
year_einsatz = self.einsatz_start.strftime('%Y')
last_number = EinsatzPublic.objects.filter(einsatznummer__isnull=False, einsatz_start__year=year_einsatz).values_list('einsatznummer', flat=True)
if EinsatzPublic.objects.filter('einsatznummer').count() >= 1:
# if last_number == None :
# last_number = 0
if last_number[:-1] != year_einsatz:
last_number = 0
einsatznummer_gen = year_einsatz + (last_number + 1)
return einsatznummer_gen
else:
einsatznummer_gen = (year_einsatz + 1)
return einsatznummer_gen
When I tried to add a record to the model (DeploymentPublic) I got the following error which I can't solve.
Internal Server Error: /super/einsatzverwaltung/einsatzpublic/add/
Traceback (most recent call last):
(...)
ValueError: too many values to unpack (expected 2)
Then I tried to see if it was the ComputedTextField and temporarily removed it. As a result, I got the following error when creating a new record:
Internal Server Error: /super/einsatzverwaltung/einsatzpublic/add/
File
(...)
"C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\db\models\base.py",
line 828, in _save_table
raise ValueError("Cannot force an update in save()
with no primary key.") ValueError: Cannot force an update in save() with no primary key.
Maybe someone here can help me because I'm really at a loss and can't find the issue.
Error Code after fixing Error 1
Commented ComputedTextField out:
Internal Server Error: /super/einsatzverwaltung/einsatzpublic/add/
Traceback (most recent call last):
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 616, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\sites.py", line 232, in inner
return view(request, *args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 1657, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 1540, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 1586, in _changeform_view
self.save_model(request, new_object, form, not add)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 1099, in save_model
obj.save()
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\db\models\base.py", line 763, in save_base
updated = self._save_table(
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\db\models\base.py", line 828, in _save_table
raise ValueError("Cannot force an update in save() with no primary key.")
ValueError: Cannot force an update in save() with no primary key.
[09/Jun/2021 18:35:45] "POST /super/einsatzverwaltung/einsatzpublic/add/ HTTP/1.1" 500 133976
With the ComputedText Field
Traceback (most recent call last):
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 616, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\sites.py", line 232, in inner
return view(request, *args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 1657, in add_view
return self.changeform_view(request, None, form_url, extra_context)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 1540, in changeform_view
return self._changeform_view(request, object_id, form_url, extra_context)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 1586, in _changeform_view
self.save_model(request, new_object, form, not add)
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\contrib\admin\options.py", line 1099, in save_model
obj.save()
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\db\models\base.py", line 726, in save
self.save_base(using=using, force_insert=force_insert,
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\db\models\base.py", line 750, in save_base
pre_save.send(
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\dispatch\dispatcher.py", line 180, in send
return [
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\django\dispatch\dispatcher.py", line 181, in <listcomp>
(receiver, receiver(signal=self, sender=sender, **named))
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\computed_property\fields.py", line 61, in resolve_computed_field
setattr(instance, self.get_attname(), self.calculate_value(instance))
File "C:\Users\marce\.virtualenvs\backend-ZTyOc35O\lib\site-packages\computed_property\fields.py", line 73, in calculate_value
instance_compute_object = getattr(instance, self.compute_from)
File "D:\04-Dev\Projekt\feuerwehr-ensdorf-webapp-cms\backend\einsatzverwaltung\models.py", line 141, in einsatznummer_calc
if last_number >= 1:
TypeError: '>=' not supported between instances of 'QuerySet' and 'int'
[09/Jun/2021 18:44:05] "POST /super/einsatzverwaltung/einsatzpublic/add/ HTTP/1.1" 500 150838
EinsatzPublic.objects.filter('einsatznummer').count() >= 1 makes no sense, since you can not filter with a string.
You should work with a (or multiple) Q objects, and/or parameters like you did when filtering the line above. If it is the same as last_number, you can reuse this queryset:
#property
def einsatznummer_calc(self):
year_einsatz = self.einsatz_start.strftime('%Y')
last_number = EinsatzPublic.objects.filter(einsatznummer__isnull=False, einsatz_start__year=year_einsatz).values_list('einsatznummer', flat=True)
if last_number:
# if last_number == None :
# last_number = 0
if last_number[:-1] != year_einsatz:
last_number = 0
einsatznummer_gen = year_einsatz + (last_number + 1)
return einsatznummer_gen
else:
einsatznummer_gen = (year_einsatz + 1)
return einsatznummer_gen

Using external script in Views.py

I have a file that sends a request to an API and retrieves information. Let's call this file get_info.py. I am now building a GUI that uses Django and the views.py file, with methods 'GET' and 'POST'.
I am now importing the function from get_info.py into views.py and using it as follows
from get_info import get_info
#api_view(['GET'])
def generate_route(request):
"""
:param request:
1. lat: posx
2. lng: pos,
3. r: radius in km
4. strategy,
5. edge_num,
6. deep,
:return:
"""
posx = request.query_params.get('lat', None)
posy= request.query_params.get('lng', None)
r= request.query_params.get('r', None)
strategy = request.query_params.get('strategy', None)
strategy = strategy if strategy else 3
edge_num = request.query_params.get('edge_num', None)
edge_num = edge_num if edge_num else 3
deep = request.query_params.get('deep', None)
deep = deep if deep else 3
print("BEFORE", posx, posy, r, strategy, edge_num, deep)
route = get_info(posx, posy, r)
print("AFTER", route)
if request.query_params.get('lat', None) is not None \
and request.query_params.get('lng', None) is not None \
and request.query_params.get('r', None) is not None:
return Response({}, status=status.HTTP_200_OK)
else:
return Response({
"Error": 'Need lat, lng, and r {}'.format(request.query_params.get('lat', None))
}, status=status.HTTP_400_BAD_REQUEST)
```
However, I get the response
> (u'BEFORE', u'112.34', u'14.55', u'300.3', 3, 3, 3)
Internal Server Error: /app/api/v1/get_info/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 483, in dispatch
response = self.handle_exception(exc)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 443, in handle_exception
self.raise_uncaught_exception(exc)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 480, in dispatch
response = handler(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/decorators.py", line 53, in handler
return func(*args, **kwargs)
File "/home/user/Projects/app/views.py", line 750, in generate_route
route = get_info(posx, posy, r)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 466, in dispatch
request = self.initialize_request(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rest_framework/views.py", line 370, in initialize_request
parser_context=parser_context
File "/usr/local/lib/python2.7/dist-packages/rest_framework/request.py", line 159, in __init__
.format(request.__class__.__module__, request.__class__.__name__)
AssertionError: The `request` argument must be an instance of `django.http.HttpRequest`, not `__builtin__.unicode`.
But when I use from django.http import HttpRequest to build my request, it tells me 'maximum depth exceeded'.
The get_info method is quite long, but in a nutshell it looks like this:
def get_info(posx, posy, r, strategy=3, edge_num=0.8, deep=0):
req_url = "http://api.map.baidu.com/direction/v2/driving?origin=posx..."
trip = requests.get(req_url).json()
return trip
When I run this get_info method in my python shell, it returns the desired trip.
If you look closely rest framework is the one which is causing the problem, If the getinfo is an APIView then it might need request as its first argument not posx which is a string.

getting error while using queryset Q in django view

I am creating the dynamic queryset using Q
while i am printing
build_query[:-1]
i am getting output
Q(owner_id=1)|Q(assigned_to=1)
but when using this in to
consultants = Consultant.objects.filter(*build_query[:-1])
getting an error too many values to unpack
i tried it with ** and without * still its not working.
when i check the type of
print type(build_query[:-1])
i am getting string type. Is the a main cause?
Traceback
Internal Server Error: /api/consultants/my_consultants/
Traceback (most recent call last):
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 57, in wrapped_view
return view_func(*args, **kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/rest_framework/viewsets.py", line 85, in view
return self.dispatch(request, *args, **kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/rest_framework/views.py", line 451, in dispatch
response = self.handle_exception(exc)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/rest_framework/views.py", line 448, in dispatch
response = handler(request, *args, **kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/ckiller/consultants/api.py", line 170, in my_consultants
consultants = Consultant.objects.filter(*build_query[:-1])
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
Q(owner_id=1)|Q(assigned_to=1)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 691, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/db/models/query.py", line 709, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/jagmeet/consultadd_workspace/devenv/local/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1331, in add_q
clause, require_inner = self._add_q(where_part, self.used_aliases)
consultants = Consultant.objects.filter(*build_query[:-1])
Did u try:
Consultant.objects.filter(*[build_query[:-1],])
args should be a list

Categories